如何才能在多维数组中仅查找具有子关键字的关键字


How can I find only keys from a multidimensional array that have a key as a child?

我的代码正在多维数组中查找具有特定字符的所有键:

<?
$array = ["farm"=>
              [
                "horse"=>
                 [
                  "rabbit"=>
                     [
                      "name"=> "alan",
                      "age"=> "21",
                      "size"=> "166",
                      "gender"=> "f"
                    ], 
              ]
           ]   
    ]; 
    function createList($array, $keySearch, $path=null) {
                  $result = [];
        foreach ($array as $key => $item) { 
            $basePath = $path === null ? $key : $path. "/" . $key;
            if (stripos($key, $keySearch) !== false)
                 $result[] = ['key' => $key, 'basePath' => $basePath];            
            if(is_array($item))
                 $result = array_merge($result, createList($item, $keySearch, $basePath));
        }
    return $result;
    }
    $keySearch = 'a';
    $res = createList($array, $keySearch);
    print_r($res);           

https://eval.in/573212

我的输出:

Array
(
    [0] => Array
        (
            [key] => farm
            [basePath] => farm
        )
    [1] => Array
        (
            [key] => rabbit
            [basePath] => farm/horse/rabbit
        )
    [2] => Array
        (
            [key] => name
            [basePath] => farm/horse/rabbit/name
        )
    [3] => Array
        (
            [key] => age
            [basePath] => farm/horse/rabbit/age
        )
)

但我只需要钥匙,小时候就有钥匙。所以在这种情况下,我的结果应该是:

Array
(
    [0] => Array
        (
            [key] => farm
            [basePath] => farm
        )
    [1] => Array
        (
            [key] => rabbit
            [basePath] => farm/horse/rabbit
        )
)

可能这就是您想要的,请检查并告诉我。只需在if条件中使用is_array($item)来检查您的项目是否有子项。

在线检查

只需更改功能:

function createList($array, $keySearch, $path=null) {
    $result = [];
    foreach ($array as $key => $item) { 
        $basePath = $path === null ? $key : $path. "/" . $key;
        if (stripos($key, $keySearch) !== false && is_array($item)){
            $result[] = ['key' => $key, 'basePath' => $basePath];
        }       
        if(is_array($item))
            $result = array_merge($result, createList($item, $keySearch, $basePath));
    }
    return $result;
}

$basePath = $path === null ? $key : $path. "/" . $key; 之后使用if(is_array($item))

$array = ["farm"=>
              [
                "horse"=>
                 [
                  "rabbit"=>
                     [
                      "name"=> "alan",
                      "age"=> "21",
                      "size"=> "166",
                      "gender"=> "f"
                    ], 
              ]
           ]   
    ]; 
function createList($array, $keySearch, $path=null) {
              $result = [];
    foreach ($array as $key => $item) { 
        $basePath = $path === null ? $key : $path. "/" . $key;
        if(is_array($item))
        if (stripos($key, $keySearch) !== false)
             $result[] = ['key' => $key, 'basePath' => $basePath];            
        if(is_array($item))
             $result = array_merge($result, createList($item, $keySearch, $basePath));
    }
    return $result;
}
$keySearch = 'a';
$res = createList($array, $keySearch);
print_r($res);

这会给你

Array
(
    [0] => Array
        (
            [key] => farm
            [basePath] => farm
        )
    [1] => Array
        (
            [key] => rabbit
            [basePath] => farm/horse/rabbit
        )
)

现场演示

尝试将if (stripos($key, $keySearch) !== false)更改为if (stripos($key, $keySearch) !== false && is_array($item))