php - Searching in an Array -
i have array:
$fruits = array( [0] => array('name'=>'banana', 'color'=>'yellow' , 'shape'=>'cylinder'), [1] => array('name'=>'apple', 'color'=>'red' , 'shape'=>'sphere'), [2] => array('name'=>'orange', 'color'=>'orange' , 'shape'=>'sphere') )
how can find out if array $fruits
contains apple
in it?
i have tried: in_array("apple", $fruits)
, didn't work out.
i tried various syntax , messed bit array_key_exists()
, nothing worked out. ideas?
php notoriously unwieldy in such cases. best all-around solution simple foreach
:
$found = false; foreach ($fruits $fruit) { if ($fruit['name'] == 'apple') { $found = true; break; } } if ($found) ....
you can write in number of sexier ways, of them require additional memory allocation and/or slower; number of them more difficult understand if not experienced.
Comments
Post a Comment