how to get the 3rd key in this array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
geoffcox
Forum Newbie
Posts: 2
Joined: Sat May 30, 2009 6:40 pm

how to get the 3rd key in this array?

Post by geoffcox »

Hello,

I'm stuck!

I want to get the 3rd key in this associative array, and call it say
$third,

groupname -> C2
usercode -> A001X001
mysql_c21answer -> 1
mysql_c21correctanswer -> 2

so that I can use

$first_num = substr($third, 8, 1);

to get the 8th character, the number following c2, in $third..

Help!

Cheers,

Geoff
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Re: how to get the 3rd key in this array?

Post by SidewinderX »

I'm not exactly sure what you are asking, but this reference should be loads of help. http://us3.php.net/manual/en/language.types.array.php
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: how to get the 3rd key in this array?

Post by McInfo »

Maybe this?

Code: Select all

<?php
$stuff = array
(   'groupname' => 'C2'
,   'usercode' => 'A001X001'
,   'mysql_c21answer' => 1
,   'mysql_c21correctanswer' => 2
);
 
$keys = array_keys($stuff);
/*
$keys => Array
(
    [0] => groupname
    [1] => usercode
    [2] => mysql_c21answer
    [3] => mysql_c21correctanswer
)
*/
 
echo substr($keys[2], 8, 1); // 1
?>
Edit: This post was recovered from search engine cache.
Last edited by McInfo on Tue Jun 15, 2010 8:19 pm, edited 1 time in total.
geoffcox
Forum Newbie
Posts: 2
Joined: Sat May 30, 2009 6:40 pm

Re: how to get the 3rd key in this array?

Post by geoffcox »

McInfo wrote:Maybe this?

Code: Select all

<?php
$stuff = array
(   'groupname' => 'C2'
,   'usercode' => 'A001X001'
,   'mysql_c21answer' => 1
,   'mysql_c21correctanswer' => 2
);
 
$keys = array_keys($stuff);
/*
$keys => Array
(
    [0] => groupname
    [1] => usercode
    [2] => mysql_c21answer
    [3] => mysql_c21correctanswer
)
*/
 
echo substr($keys[2], 8, 1); // 1
?>

many thanks - someone in comp.lang.php came up with the same idea - and it is doing the job!

Cheers

Geoff
Post Reply