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
how to get the 3rd key in this array?
Moderator: General Moderators
-
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?
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?
Maybe this?
Edit: This post was recovered from search engine cache.
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
?>
Last edited by McInfo on Tue Jun 15, 2010 8:19 pm, edited 1 time in total.
Re: how to get the 3rd key in this array?
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