Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If I understand your problem correctly, this should work. Notice how I call the <code>orderMe</code> function inside the function to make it recursive.</p> <pre><code>function orderMe($input, $parentId) { $return = array($parentId =&gt; array('itemGroupID' =&gt; $parentId)); $childs = array(); foreach ($input as $i) { if ($i['itemGroupID'] == $parentId) { $return[$i['itemGroupID']]['children'][$i['childItemGroupID']] = array('itemGroupID' =&gt; $i['childItemGroupID']); $childs[] = $i['childItemGroupID']; } if (in_array($i['childItemGroupID'], $childs)) { $allChilds = orderMe($input, $i['childItemGroupID']); if (!empty($allChilds[$i['childItemGroupID']]['children'])) $return[$i['itemGroupID']]['children'][$i['childItemGroupID']] = $allChilds; } } return $return; } print_r(orderMe($input, 1)); </code></pre> <p>Outputs:</p> <pre><code>array ( 1 =&gt; array ( 'itemGroupID' =&gt; 1, 'children' =&gt; array ( 2 =&gt; array ( 'itemGroupID' =&gt; 2, ), 3 =&gt; array ( 'itemGroupID' =&gt; 3, ), 4 =&gt; array ( 'itemGroupID' =&gt; 4, ), 212 =&gt; array ( 'itemGroupID' =&gt; 212, ), 339 =&gt; array ( 'itemGroupID' =&gt; 339, ), 336 =&gt; array ( 'itemGroupID' =&gt; 336, ), 6 =&gt; array ( 6 =&gt; array ( 'itemGroupID' =&gt; 6, 'children' =&gt; array ( 8 =&gt; array ( 'itemGroupID' =&gt; 8, ), 9 =&gt; array ( 9 =&gt; array ( 'itemGroupID' =&gt; 9, 'children' =&gt; array ( 15 =&gt; array ( 'itemGroupID' =&gt; 15, ), ), ), ), 10 =&gt; array ( 10 =&gt; array ( 'itemGroupID' =&gt; 10, 'children' =&gt; array ( 16 =&gt; array ( 'itemGroupID' =&gt; 16, ), ), ), ), 11 =&gt; array ( 11 =&gt; array ( 'itemGroupID' =&gt; 11, 'children' =&gt; array ( 17 =&gt; array ( 'itemGroupID' =&gt; 17, ), ), ), ), 12 =&gt; array ( 12 =&gt; array ( 'itemGroupID' =&gt; 12, 'children' =&gt; array ( 18 =&gt; array ( 'itemGroupID' =&gt; 18, ), ), ), ), 13 =&gt; array ( 13 =&gt; array ( 'itemGroupID' =&gt; 13, 'children' =&gt; array ( 19 =&gt; array ( 'itemGroupID' =&gt; 19, ), ), ), ), 74 =&gt; array ( 74 =&gt; array ( 'itemGroupID' =&gt; 74, 'children' =&gt; array ( 75 =&gt; array ( 'itemGroupID' =&gt; 75, ), ), ), ), ), ), ), 5 =&gt; array ( 'itemGroupID' =&gt; 5, ), ), ), ) </code></pre>
 

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