简体   繁体   English

如何提取具有给定结构且键=> value匹配多维度数组给定值的数组?

[英]How to extract an array that has a given structure and which's key => value matches a given value of an mutlidimensional array?

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: $mainarray的结构如下:

'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' . 每个子childarray可以具有2个直接子keys'links'和可选的'below' Within links there is always a key 'mykey' , which is the only key that I need to check. links中始终有一个key 'mykey' ,这是我需要检查的唯一密钥。 If a child array has ['links']['mykey'] == 'valueofinterest' , I'd like to have this element returned , like $sub = $mainarray['child1']['below']['child11']['below']['childofinterest'] . 如果子数组具有['links']['mykey'] == 'valueofinterest'我想返回此元素 ,例如$sub = $mainarray['child1']['below']['child11']['below']['childofinterest']
'below' means that the childarray has childs itself which can also have below arrays (sub menu..). 'below'表示子数组本身具有子数组,也可以具有以下数组(子菜单..)。

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). 我的问题是我尝试找到的子数组可以位于任何其他子数组的'below'键中,但我不知道深度(虽然不太深,但可以变化)。 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. 我试图弄乱foreach循环和while循环并将它们组合在一起,但我只是弄不清楚如何获取子数组。 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 :( 我试图递归走,但不知道如何返回元素:(

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 更新V2

Fixed the function, so it works now 修复了该功能,因此现在可以使用

Try like this, 这样尝试

if (array_key_exists('keyvalue', $array)) {
    $subarray = $array['keyvalue'];
    return $subarray;
}

It will return sub array 它将返回子数组

这里是。

$finalarray = array_map(create_function('$yourarray', 'return $yourarray["arrayindex"];'), $actualarray );

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

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