Php multidimensional array search
Possible duplicate:
php. to increase a multidimensional array?
I have a multidimensional binding that contains arrays and objects. I have to look for one meaning in it. How can I do?
view Object
(
[db_table] => views_view
[base_table] => users
[args] => Array
(
)
[use_ajax] =>
[result] => Array
(
)
[pager] => Array
(
[use_pager] =>
[items_per_page] => 10
[element] => 0
[offset] => 0
[current_page] => 0
)
[old_view] => Array
(
[0] =>
)
[vid] => 1
[name] => userone
[description] => userone
[tag] =>
[view_php] =>
[is_cacheable] => 0
[display] => Array
(
[default] => views_display Object
(
[db_table] => views_display
[vid] => 1
[id] => default
[display_title] => Defaults
[display_plugin] => default
[position] => 1
[display_options] => Array
(
Likewise, the array continues. How can I search if one value exists?
0
source to share
1 answer
If you want to know if some specific value and something else exists, it's trivial using recursive iterators :
$found = false;
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value) {
if ($value == 'theValueImLookingFor') {
$found = true;
break;
}
}
It's easy to write this in a recursive function:
function recursive_in_array($array, $needle) {
foreach ($array as $value) {
$isIterable = is_object($value) || is_array($value);
if ($value == $needle || ($isIterable && recursive_in_array($value, $needle))) {
return true;
}
}
return false;
}
+2
source to share