Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to extract an array that has a given structure and which's key => value matches a given value of an mutlidimensional array?
    primarykey
    data
    text
    <p>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.</p> <p>The structure of <code>$mainarray</code> is like this:</p> <pre><code>'childarray1' =&gt; array( 'link' =&gt; array( .. 'mykey' =&gt; 'valueofinterest' ), 'below' =&gt; array() of childarrays ), 'childarray2' =&gt; array( 'link' =&gt; array( .. 'mykey' =&gt; 'somevalue' ) ), 'childarray3' =&gt; array( 'link' =&gt; array( .. 'mykey' =&gt; 'someothervalue' ), 'below' =&gt; array() of childarrays ) </code></pre> <p>Each <code>childarray</code> can have 2 direct child <code>keys</code>, <code>'links'</code> and optionally <code>'below'</code>. Within <code>links</code> there is always a <code>key 'mykey'</code>, which is the only key that I need to check. If a child array has <code>['links']['mykey'] == 'valueofinterest'</code>, <strong>I'd like to have this element returned</strong>, like <code>$sub = $mainarray['child1']['below']['child11']['below']['childofinterest']</code>.<br> <code>'below'</code> means that the childarray has childs itself which can also have below arrays (sub menu..).</p> <p>My hude problem is that the childarray I try to find can be in any other childarrays'S <code>'below'</code> 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:</p> <pre><code>$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' =&gt; array( // 'link' =&gt; array( // .. // 'mykey' =&gt; 'xxx' // ), // 'below' =&gt; array() of childarrays which i NEEED :) // // ) return $sub; } </code></pre> <p>I've tried to walk recursively but cant figure out how to return the element :(</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload