繁体   English   中英

使用路径在PHP数组中进行递归搜索

[英]Recursive search in PHP array with path

我有这个haystack数组:

$array = [
   [
      "name" => "Intro",
      "id" => "123",
      "children" => [
         "name" => "foo",
         "id" => "234",
         "children" => [
            "name" => "mur",
            "id" => "445",
         ]
      ]
   ],[
      "name" => "chapter one",
      "id" => "9876",
      "children" => [
         "name" => "foo",
         "id" => "712",
         "children" => [
            "name" => "bar",
            "id" => "888",
         ]
      ]
   ]
];

而这个针阵列: $needle = ["chapter one","foo","bar"]

我正在研究一个递归搜索函数,它将返回跟随$needle路径的列name匹配的子元素的id值。

在示例中,它应该返回888 到目前为止,我有这个,但我没有找到如何遵循 $ needle路径,而不是找到值,假设它们是唯一的。 感谢任何让我走上正轨的帮助。

function searchTree( $needle, $haystack, $strict=false, $path=array() )
{
   if( !is_array($haystack) ) {
      return false;
   }

   foreach( $haystack as $key => $val ) {
   if( is_array($val) && $subPath = searchTree($needle, $val, $strict, $path) ) {
      $path = array_merge($path, array($key), $subPath);
      return $path;
   } elseif( (!$strict && $val == $needle) || ($strict && $val === $needle) ) {
      $path[] = $key;
      return $path;
   }
    }
    return false;
}

我会尝试清理一下,但这有效:

$needle = ["chapter one", 'foo', 'bar'];
$array = [
    [
        "name" => "Intro",
        "id" => "123",
        "children" => [
            "name" => "foo",
            "id" => "234",
            "children" => [
                "name" => "mur",
                "id" => "445",
            ]
        ]
    ],[
        "name" => "chapter one",
        "id" => "9876",
        "children" => [
            "name" => "foo",
            "id" => "712",
            "children" => [
                "name" => "bar",
                "id" => "888",
            ]
        ]
    ]
];

function searchTree($needle, $haystack, $strict=false) {
    if(!is_array($haystack)) {
        return false;
    }
    $match = false;
    if(array_keys($haystack) !== range(0, count($haystack) - 1) && !empty($needle)) {
        if(($strict && $haystack['name'] === $needle[0]) || (!$strict && $haystack['name'] == $needle[0])) {
            $match = true;
            array_shift($needle);
            if (!empty($needle)) {
                return searchTree($needle, $haystack['children'], $strict);
            }
        }
    } else {
        foreach ($haystack as $key => $value) {
            if (is_array($value) && !empty($needle)) {
                if (($strict && $value['name'] === $needle[0]) || (!$strict && $value['name'] == $needle[0])) {
                    $match = true;
                    array_shift($needle);
                    if (!empty($needle)) {
                        return searchTree($needle, $value['children'], $strict);
                    } else {
                        $haystack = $haystack[$key];
                    }
                }
            }
        }
    }
    return (isset($haystack['id']) && $match) ? $haystack['id'] : false;
}

echo searchTree($needle, $array);

输出:

888

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM