Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's what I came up with... It's not a quite efficient approach, but maybe somebody could turn it into a fancy recursive function to make it abstract - I've tried a little, but I really suck at recursions :-P</p> <pre><code>function cascade_array ($array, $current_level = 1) { // filter only the array elements with the level==current_level $chunks = array_filter ($array, function ($v) use ($current_level) { return $v['level'] == 1; } ); // extract the keys from those elements an put them in reverse order $splice_keys = array_reverse(array_keys($chunks)); // now splice the array at those keys and put the // array-parts in a result-array, so you have an array of e.g. all level1 foreach ($splice_keys as $offset) { $results[] = array_splice($array, $offset); } //now walk through the results and basically do the same foreach($results as &amp;$level) { $level[0]['children'] = array(); $array = array_splice($level, 1); $chunks = array_filter ($array, function ($v) use ($current_level) { return $v['level'] == 2; } ); $splice_keys = array_reverse(array_keys($chunks)); foreach($splice_keys as $offset) { $level[0]['children'][] = array_splice($array, $offset); } foreach($level[0]['children'] as &amp;$child_level) { $child_level[0]['children'] = array(); $array = array_splice($child_level, 1); $chunks = array_filter ($array, function ($v) use ($current_level) { return $v['level'] == 3; } ); $splice_keys = array_reverse(array_keys($chunks)); foreach($splice_keys as $offset) { $child_level[0]['children'][] = array_splice($array, $offset); } } } return $results; } </code></pre> <p>The code is actually pretty straight forward, I guess. Here's a working example: <a href="http://codepad.viper-7.com/kb5JBc" rel="nofollow">http://codepad.viper-7.com/kb5JBc</a> - I hope it helps :)</p>
 

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