note 106304 added to language.types.array
| From: | prince at 3ddatalabs dot com | Date: | Wed, 26 Oct 2011 16:35:35 +0000 |
| Subject: | note 106304 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184144@lists.php.net to get a copy of this message | ||
Hi, here's a way to access an associative array via it's numeric index.
<?php
//sample associative array:
$myAssocArray = array('a'=>'apple', 'b'=>'banana',
'c'=>'carrot', 'd'=>'dragon fruit');
echo $assocArray[0];
//Above won't work! Because you can't access an associative array numerically. But,
function goAssocArrayNumeric($arrAssoc, $key=-1)
{
$i = -1;
foreach ($arrAssoc as $k => $v)
{
$i++;
if ($i == $key)
{
return $v;
}
}
return FALSE;
}
//usage:
echo goAssocArrayNumeric($myAssocArray,0);//apple
echo goAssocArrayNumeric($myAssocArray,3);//dragon fruit
?>
Hope it helps!
Thanks.
----
Server IP: 69.147.83.197
Probable Submitter: 112.135.135.170
----
Manual Page -- http://www.php.net/manual/en/language.types.array.php
Edit -- https://master.php.net/note/edit/106304
Del: integrated -- https://master.php.net/note/delete/106304/integrated
Del: useless -- https://master.php.net/note/delete/106304/useless
Del: bad code -- https://master.php.net/note/delete/106304/bad+code
Del: spam -- https://master.php.net/note/delete/106304/spam
Del: non-english -- https://master.php.net/note/delete/106304/non-english
Del: in docs -- https://master.php.net/note/delete/106304/in+docs
Del: other reasons-- https://master.php.net/note/delete/106304
Reject -- https://master.php.net/note/reject/106304
Search -- https://master.php.net/manage/user-notes.php