简体   繁体   中英

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:

'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 :(

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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