0

Possible Duplicate:
php. walk up multidimensional array?

I have a multidimensional arrray which contains arrays and objects. I have to search one value 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
                    (

Like this the array continue. How can I search if one value exists?

Community
  • 1
  • 1
Prajila V P
  • 5,027
  • 2
  • 23
  • 36
  • http://stackoverflow.com/questions/7750420/php-walk-up-multidimensional-array/7750829#7750829 – Teneff Oct 19 '11 at 07:12

1 Answers1

2

If you only want to find out if a certain value exists and nothing else, this is trivial using recursive iterators:

$found = false;
foreach (new RecursiveIteratorIterator(new RecursiveArrayIterator($array)) as $value) {
    if ($value == 'theValueImLookingFor') {
        $found = true;
        break;
    }
}

It's not much more complex to write this up 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;
}
deceze
  • 510,633
  • 85
  • 743
  • 889
  • thanks.this is good method for array search. but my array contains both array variables and objects . this function skips the objects.So i can't use this – Prajila V P Oct 19 '11 at 08:44
  • 1
    Then you should be able to extend `recursive_in_array` to also check for `is_object` and allow passing of objects, they can be iterated using `foreach` as well. I'll leave that up as an exercise to you. – deceze Oct 19 '11 at 08:47
  • Is this coorect? But it is not working function recursive_in_array($array, $needle) { foreach ($array as $value) { //print_r($value); if ($value == $needle ) { return true; } else if(is_array($value)) { recursive_in_array($value, $needle); } else if(is_object($value)) { recursive_in_array($value, $needle); } } return false; } – Prajila V P Oct 19 '11 at 11:52
  • 1
    You only need to add `|| is_object($value)` to the `if` line and remove the `array` type hint for the `$array` parameter. The logic is "for each value in the array, if value equals needle or value is an array or object and needle is somewhere in value, return true". – deceze Oct 19 '11 at 12:13
  • i changed the code like this. Still it is not working. it gives true for all the time(if the value is not present then also). function recursive_in_array($array, $needle) { foreach ($array as $value) { if ($value == $needle || is_object($value) || (is_array($value) && recursive_in_array($value, $needle))) { return true; } } return false; } echo recursive_in_array($view, "user_reorder"); – Prajila V P Oct 19 '11 at 12:28
  • i tried this code . but if the needle is not present then also it gives me true. Actually am trying to search value in view variable in hook_views_query_alter(&$view, &$query) – Prajila V P Oct 20 '11 at 12:57