note 106312 added to language.types.array
| From: | prince at 3ddatalabs dot com | Date: | Thu, 27 Oct 2011 01:02:18 +0000 |
| Subject: | note 106312 added to language.types.array | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184156@lists.php.net to get a copy of this message | ||
Hi, you also can try goAssocArrayNumeric() on two-dimensional or three-dimensional arrays.
<?php
//Now it's a two-dimensional array
$myTwoDimArray =
array('a'=>array('a0'=>'apple','a1'=>'avocado','a2'=>'apricot'),
'b'=>array('b0'=>'banana','b1'=>'blackberry','b2'=>'breadfruit'),
'c'=>array('c0'=>'carrot','c1'=>'coco','c2'=>'coffee'));
echo $myTwoDimArray[0][0]; //won't work! But,
//using the same function used to access associative arrays numerically, we may access 2-dimensional
arrays numerically.
function goAssocArrayNumeric($arrAssoc, $key=-1)
{
$i = -1;
foreach ($arrAssoc as $k => $v)
{
$i++;
if($i == $key)
{
return $v;
}
}
return FALSE;
}
//usage: applying it twice.
echo goAssocArrayNumeric(goAssocArrayNumeric($myTwoDimArray,0),0);//apple
echo goAssocArrayNumeric(goAssocArrayNumeric($myTwoDimArray,2),1);//coco
echo goAssocArrayNumeric(goAssocArrayNumeric($myTwoDimArray,2),2);//coffee
?>
Thanks!
Prince.
----
Server IP: 69.147.83.197
Probable Submitter: 112.135.144.29
----
Manual Page -- http://www.php.net/manual/en/language.types.array.php
Edit -- https://master.php.net/note/edit/106312
Del: integrated -- https://master.php.net/note/delete/106312/integrated
Del: useless -- https://master.php.net/note/delete/106312/useless
Del: bad code -- https://master.php.net/note/delete/106312/bad+code
Del: spam -- https://master.php.net/note/delete/106312/spam
Del: non-english -- https://master.php.net/note/delete/106312/non-english
Del: in docs -- https://master.php.net/note/delete/106312/in+docs
Del: other reasons-- https://master.php.net/note/delete/106312
Reject -- https://master.php.net/note/reject/106312
Search -- https://master.php.net/manage/user-notes.php