dongzhi5386 2013-10-01 09:15
浏览 25
已采纳

如何提取具有给定结构的数组以及哪个key => value匹配多维数组的给定值?

I have an array consisting of many other arrays, which might also consist of other arrays. Its basically like a navigation hierarchy, one menu link can be a menu with sub menus and so on.

The structure of $mainarray is like this:

'childarray1' => array(
    'link' => array(
        ..
        'mykey' => 'valueofinterest'
    ),
    'below' => array() of childarrays

),
'childarray2' => array(
    'link' => array(
        ..
        'mykey' => 'somevalue'
    )

),
'childarray3' => array(
    'link' => array(
        ..
        'mykey' => 'someothervalue'
    ),
    'below' => array() of childarrays

)

Each childarray can have 2 direct child keys, 'links' and optionally 'below'. Within links there is always a key 'mykey', which is the only key that I need to check. If a child array has ['links']['mykey'] == 'valueofinterest', I'd like to have this element returned, like $sub = $mainarray['child1']['below']['child11']['below']['childofinterest'].
'below' means that the childarray has childs itself which can also have below arrays (sub menu..).

My hude problem is that the childarray I try to find can be in any other childarrays'S 'below' key, I dont know the depth (its not too deep, though it can vary). I've tried to mess with foreach loops and while loops and combining those, I just cant figure it out how to get the child array. I want to do it like this:

$value = 'xxx';

$sub = return_sub_menu($value);

function return_sub_menu($value) {
    $array = $mainarray();
    $sub = array();

    // find the child array which's ['link']['mykey'] == $value;

    // $sub is now something like:
    // 'childarray321' => array(
    //  'link' => array(
    //      ..
    //      'mykey' => 'xxx'
    //  ),
    //  'below' => array() of childarrays which i NEEED :)
    // 
    // )
    return $sub;
}

I've tried to walk recursively but cant figure out how to return the element :(

  • 写回答

3条回答 默认 最新

  • doulongdan2264 2013-10-01 09:24
    关注
    function recursiveSearch($array, $value){
      foreach($array as $sub){
        if ($sub['link']['mykey'] == $value)
          return $sub ;
    
        if (!empty($sub['below'])){
            $returned = recursiveSearch($sub['below'], $value);
            if ($returned !== null)
                return $returned ;
        }
      }
    
        return null ;
    }
    
    $sub = recursiveSearch($array, "valueofinterest"); 
    //Returns array with ['link']['mykey'] == $value ;
    var_dump($sub);
    

    UPDATE V2

    Fixed the function, so it works now

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

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?