Umesh Jakhar
Umesh Jakhar

Reputation: 23

How to search for value in array tree?

this is my array

Array
(
    [category_id] => 4
    [parent_id] => 3
    [name] => Default Category
    [is_active] => 1
    [position] => 4
    [level] => 2
    [children] => Array
        (
            [0] => Array
                (
                    [category_id] => 122
                    [parent_id] => 4
                    [name] => Root 
                    [is_active] => 1
                    [position] => 1
                    [level] => 3
                    [children] => Array
                        (
                            [0] => Array
                                (
                                    [category_id] => 123
                                    [parent_id] => 122
                                    [name] =>  Clothing 
                                    [is_active] => 1
                                    [position] => 1
                                    [level] => 4
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [category_id] => 124
                                                    [parent_id] => 123
                                                    [name] =>  Men Clothing 
                                                    [is_active] => 1
                                                    [position] => 1
                                                    [level] => 5
                                                    [children] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [category_id] => 125
                                                                    [parent_id] => 124
                                                                    [name] =>  Polos & Tees
                                                                    [is_active] => 1
                                                                    [position] => 1
                                                                    [level] => 6
                                                                    [children] => Array
                                                                        (
                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                            [1] => Array
                                (
                                    [category_id] => 126
                                    [parent_id] => 122
                                    [name] =>  Fashion 
                                    [is_active] => 1
                                    [position] => 2
                                    [level] => 4
                                    [children] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [category_id] => 127
                                                    [parent_id] => 126
                                                    [name] =>  Footwear 
                                                    [is_active] => 1
                                                    [position] => 1
                                                    [level] => 5
                                                    [children] => Array
                                                        (
                                                            [0] => Array
                                                                (
                                                                    [category_id] => 128
                                                                    [parent_id] => 127
                                                                    [name] =>  Women 
                                                                    [is_active] => 1
                                                                    [position] => 1
                                                                    [level] => 6
                                                                    [children] => Array
                                                                        (
                                                                            [0] => Array
                                                                                (
                                                                                    [category_id] => 129
                                                                                    [parent_id] => 128
                                                                                    [name] =>  Flats
                                                                                    [is_active] => 1
                                                                                    [position] => 1
                                                                                    [level] => 7
                                                                                    [children] => Array
                                                                                        (
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

and what I want to do is that : write a function

foo($find,$array){
    //do some coding to search in the array
    return $category_id;//category id of the coresponding matched name in array 
}

for example : foo("Clothing",$array); will return 18 foo("Men Clothing",$array) will return 19 and so on

Upvotes: 2

Views: 4422

Answers (2)

Pedro Amaral Couto
Pedro Amaral Couto

Reputation: 2115

This is a solution with a recursion:

function foo($find, $array) {
    if( $array['name'] == $find ) {
        return $array['category_id'];
    }

    if( empty($array['children']) ) {
        return null;
    }

    foreach($array['children'] as $child) {
        $result = foo($find, $child);
        if( $result !== null ) {
            return $result;
        }
    }

    return null;
}

echo foo('Default Category', $array), "\n"; // 4
echo foo('Root', $array), "\n"; // 122
echo foo('Clothing', $array), "\n"; // 123
echo foo('Men Clothing', $array), "\n"; // 124
echo foo('Polos & Tees', $array), "\n"; // 125
echo foo('Fashion', $array), "\n"; // 126
echo foo('Footwear', $array), "\n"; // 127
echo foo('Women', $array), "\n"; // 128
echo foo('Flats', $array), "\n"; // 129 

Upvotes: 1

MH2K9
MH2K9

Reputation: 12039

You can use recursion. An example here..

function getId($arr, $val){
    if(is_array($arr)){
        if(isset($arr['name']) && trim($arr['name']) == trim($val)){
            return isset($arr['category_id']) ? $arr['category_id'] : 'Not found';
        }
        foreach($arr as $values){
            if(is_array($values)){
                return getId($values, $val);
            }
        }
    }
}

$val = getId($arr, 'Polos & Tees');
echo $val; //output 20

Upvotes: 1

Related Questions