3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $resultset = [ ['id' => '1a', 'parent' => null], ['id' => '2a', 'parent' => null], ['id' => '3a', 'parent' => '1a'], ['id' => '4a', 'parent' => null], ['id' => '5a', 'parent' => '3a'], ['id' => '6a', 'parent' => '5a'], ['id' => '7a', 'parent' => null], ['id' => '8a', 'parent' => '3a'] ]; function findParent(&$array, $parent = 0, $children = []){ // make $array modifiable foreach ($array as $i => &$row) { // make $row modifiable if ($parent) { // if not zero if ($i == $parent) { // found parent unset($array[$i]['parent']); $row = $children; // append child to parent's children subarray } elseif (!empty($row)) { // go down rabbit hole looking for parent findParent($row, $parent, $children); // look deeper for parent while preserving the initial parent_id and row // $row = $row['children']; } // else continue; } elseif ($row['parent']) { // child requires adoption unset($array[$i]); // remove child from level because it will be store elsewhere and won't be its own parent (reduce iterations in next loop & avoid infinite recursion) findParent($array, $row['parent'], $row); // look for parent using parent_id while carrying the entire row as the childarray } // else continue; } return $array; // return the modified array } var_export(findParent($resultset));
based on tdj4B
Output for 5.6.38 - 7.3.0rc3
Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 Warning: Invalid argument supplied for foreach() in /in/8LBIR on line 15 array ( 0 => array ( 'id' => '1a', 'parent' => NULL, ), 1 => array ( 'id' => '3a', 'parent' => '1a', ), 3 => array ( 'id' => '8a', 'parent' => '3a', ), 6 => array ( 'id' => '7a', 'parent' => NULL, ), )