如何搜索父键的多维数组

时间:2018-01-10 11:30:58

标签: php arrays multidimensional-array

我正在尝试搜索多维数组,当我搜索一个值时,它应该返回它的父键。数组看起来像这样:

[
  "fruits" => [
     "sweet" => [
       "apple",
       "banana",
     ],
     "citrus" => [
       "lemon",
       "orange",
     ]
   ],
   "vegetables" => [
     "leafy" => [
       "spinach",
       "broccoli",
     ]
   ],
]

我希望函数在我搜索leafy时返回broccoli,或者如果我搜索leafy,那么它应该返回vegetables但是此函数总是返回null 1}}:

function recursiveFind(array $haystack, $needle)
{
    $foundKey = null;
    foreach ($haystack as $key => $value) {        

        if(is_array($value)) {
            if(in_array($needle, $value)){
                return $key;
            } else {
                $foundKey = recursiveFind($value, $needle);
            }
        }

    }
    return $foundKey;
}

我尝试的另一个功能如下所示,它总是返回false

function recursiveFind(array $haystack, $needle)
{
        foreach($haystack as $key => $value) {
            $current_key = $key;
            if($needle === $value || (is_array($value) && recursiveFind($value, $needle) !== false)) {
                return $current_key;
            }
        }
        return false;
}

如果我们只删除水果和蔬菜包装,只有第二级时,上面的功能才有效。

3 个答案:

答案 0 :(得分:3)

请检查此更新的代码

<?php

$data = [
    "fruits" => [
        "sweet" => [
            "apple",
            "banana",
        ],
        "citrus" => [
            "lemon",
            "orange",
        ]
    ],
    "vegetables" => [
        "leafy" => [
            "spinach",
            "broccoli",
        ]
    ],
];

function recursiveFind(array $datas, $needle) {
    foreach ($datas as $key => $data) {
        foreach ($data as $inx => $stx) {
            if ($needle == $inx) {
                return $key;
            }
            if (in_array($needle, $stx)) {
                return $inx;
            }
        }
    }
    return null;
}

echo $parentkey = recursiveFind($data, 'broccoli'); // output: leafy
echo $parentkey = recursiveFind($data, 'leafy'); // output: vegetables
?>

答案 1 :(得分:1)

function recursiveFind(array $haystack, $needle)
{
   foreach($haystack as $key => $data){
        foreach($data as $k=>$v){
            if($needle==$k){
                return $key;
            }
            if(in_array($needle, $v)){
                return $k;
            }
        }
    }
    return null;
}

根据评论中指定的要求 -

 public $needle = 'citrus';

private function recursiveFind($haystack, $prev)
{
    foreach ($haystack as $key => $data) {
        if (is_array($data) && in_array($this->needle, $data)) {
            return $key;
        }
        if ($this->needle === $key) {
            return $prev;
        }
        if (is_array($data)) {
            $result = $this->recursiveFind($data, $key);
            if ($result != null)
                return $result;
        }
    }

    return null;
}

并称之为 -

$value = $this->recursiveFind($data, null);

return $value;

请注意,我已将$needle声明为类变量。您现在可以设置此字段。

Happy Coding :)

答案 2 :(得分:0)

function recursive_return_array_value_by_key($needle, $haystack){
   $return = false;
   foreach($haystack as $key => $val){
      if(is_array($val)){
         $return = recursive_return_array_value_by_key($needle, $val);
      }
      else if($needle === $key){
        return "$val\n";
      }
    }
    return $return;
  }