This is another way to get value from a multidimensional array, but for versions of php >= 5.3.x
<?php
function array_value_recursive($key, array $arr){
$val = array();
array_walk_recursive($arr, function($v, $k) use($key, &$val){
if($k == $key) array_push($val, $v);
});
return count($val) > 1 ? $val : array_pop($val);
}
$arr = array(
'foo' => 'foo',
'bar' => array(
'baz' => 'baz',
'candy' => 'candy',
'vegetable' => array(
'carrot' => 'carrot',
)
),
'vegetable' => array(
'carrot' => 'carrot2',
),
'fruits' => 'fruits',
);
var_dump(array_value_recursive('carrot', $arr)); var_dump(array_value_recursive('apple', $arr)); var_dump(array_value_recursive('baz', $arr)); var_dump(array_value_recursive('candy', $arr)); var_dump(array_value_recursive('pear', $arr)); ?>