Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Please, try the following code and let me know the results.</p> <p>You just need to pass your array to the <code>find_deepest</code> function.</p> <pre><code>function find_deepest( $array ) { $index = ''; // this variable stores the current position (1.2, 1.3.2, etc.) $include = true; // this variable indicates if the current position should be added in the result or not $result = array(); // this is the result of the function, containing the deepest indexes $array_stack = array(); // this is a STACK (or LIFO) to temporarily store the sub-arrays - see http://en.wikipedia.org/wiki/LIFO_%28computing%29 reset( $array ); // here we make the array internal POINTER move to the first position // each loop interaction moves the $array internal pointer one step forward - see http://php.net/each // if $current value is null, we reached the end of $array; in this case, we will also continue the loop, if the stack contains more than one array while ( ( $current = each( $array ) ) || ( count( $array_stack ) &gt; 1 ) ) { // we are looping $array elements... if we find an array (a sub-array), then we will "enter it... if ( is_array( $current['value'] ) ) { // update the index string $index .= ( empty ( $index ) ? '' : '.' ) . $current['key']; // we are entering a sub-array; by default, we will include it $include = true; // we will store our current $array in the stack, so we can move BACK to it later array_push( $array_stack, $array ); // ATTENTION! Here we CHANGE the $array we are looping; here we "enter" the sub-array! // with the command below, we start to LOOP the sub-array (whichever level it is) $array = $current['value']; } // this condition means we reached the END of a sub-array (because in this case "each()" function has returned NULL) // we will "move out" of it; we will return to the previous level elseif ( empty( $current ) ) { // if we reached this point and $include is still true, it means that the current array has NO sub-arrays inside it (otherwise, $include would be false, due to the following lines) if ( $include ) $result[] = $index; // ATTENTION! With the command below, we RESTORE $array to its precedent level... we entered a sub-array before, now we are goin OUT the sub-array and returning to the previous level, where the interrupted loop will continue $array = array_pop( $array_stack ); // doing the same thing with the $index string (returning one level UP) $index = substr( $index, 0, strrpos( $index, '.' ) ); // if we are RETURNING one level up, so we do NOT want the precedent array to be included in the results... do we? $include = false; } // try removing the comment from the following two lines! you will see the array contents, because we always enter this "else" if we are not entering a sub-array or moving out of it // else // echo $index . ' =&gt; ' . $current['value'] . '&lt;br&gt;'; } return $result; } $result = find_deepest( $my_array ); print_r( $result ); </code></pre> <p>The most important parts of the code are:</p> <ol> <li>the <code>each</code> command inside the <code>while</code> loop</li> <li>the <code>array_push</code> function call, where we store the current array in the "array stack" in order to return back to it later</li> <li>the <code>array_pop</code> function call, where we return one level back by restoring the current array from the "array stack"</li> </ol>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      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