Page 1 of 1
how to get the 3rd key in this array?
Posted: Sat May 30, 2009 6:41 pm
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
Re: how to get the 3rd key in this array?
Posted: Sat May 30, 2009 9:49 pm
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
Re: how to get the 3rd key in this array?
Posted: Sun May 31, 2009 11:31 am
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.
Re: how to get the 3rd key in this array?
Posted: Sun May 31, 2009 4:58 pm
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