Php 如何在数组树中搜索值?

Php 如何在数组树中搜索值?,php,arrays,search,tree,nested,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 ( [cate

这是我的阵列

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(“衣服”,美元数组)将返回18
foo(“男装”,$array)
将返回19
依此类推

您可以使用
递归
。这里有一个例子

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

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

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 

我认为这是因为循环不会经过所有的“子”节点。我的代码适用于所有值。
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