douwaz34842 2018-01-10 11:30
浏览 22
已采纳

如何搜索父键的多维数组

I am trying to search a multidimensional array, where when I search for a value it should return me its parent key. Array looks like this:

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

I want the function to return leafy when I search for broccoli or if I search for leafy then it should return vegetables but this function always return me null:

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;
}

One more function I tried is as below, which returns me false always:

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;
}

Above function works when there is only second level for example if we remove fruits and vegetable wrappers.

  • 写回答

3条回答 默认 最新

  • dop82210 2018-01-10 11:42
    关注
    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;
    }
    

    As per your requirement specified in comments-

     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;
    }
    

    And call this like-

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

    Notice that I have declared $needle as a class variable. You can set this field anything you want now.

    Happy Coding :)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了