Jump to content

How To Search Get Key From Multidimensional Array


JustinK101

Recommended Posts

How do I search for a given `id` in the following multidimensional array. Let's say I want to return the ram `value` for a given `id` key of 1. So it should get back 256. Thanks.

 

Array ( [ 0 ] => Array ( [id] => 1 [name] => 256 [price] => 2000 [ram] => 256 ) [1] => Array ( [id] => 2 [name] => 512 [price] => 3800 [ram] => 512 ) [2] => Array ( [id] => 3 [name] => 1GB [price] => 7000 [ram] => 1024 ) [3] => Array ( [id] => 4 [name] => 2GB [price] => 13000 [ram] => 2048 ) [4] => Array ( [id] => 5 [name] => 4GB [price] => 25000 [ram] => 4096 ) [5] => Array ( [id] => 6 [name] => 8GB [price] => 45000 [ram] => 8192 ) [6] => Array ( [id] => 7 [name] => 15.5GB [price] => 80000 [ram] => 15872 ) [7] => Array ( [id] => 107 [name] => 384 [price] => 2500 [ram] => 384 ) [8] => Array ( [id] => 108 [name] => 768 [price] => 4900 [ram] => 768 ) [9] => Array ( [id] => 109 [name] => 1.5GB [price] => 10000 [ram] => 1536 ) [10] => Array ( [id] => 110 [name] => 3GB [price] => 19000 [ram] => 3072 ) )

Just a tip to display your array nicer, maybe even more sugestive:

echo "<pre>";
print_r($arr);
echo "<pre>";

 

And your problem can be easily solved with a foreach loop:

foreach($arr as $index=>$item){
echo $item['id'];
//also available for further manipulation	
}

 

Hope this helps!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.