如何在数组树中搜索值?

时间:2014-10-02 16:13:07

标签: php arrays search tree nested

这是我的数组

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
                                                                                        (
                                                                                        )

                                                                                )

                                                                        )

                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

和 我想做的是: 写一个函数

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

例如: foo("Clothing",$array);将返回18 foo("Men Clothing",$array)将返回19 等等

2 个答案:

答案 0 :(得分:1)

这是一个带递归的解决方案:

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 

答案 1 :(得分:0)

您可以使用recursion。这里有一个例子..

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