3v4l.org

run code in 300+ PHP versions simultaneously
<?php function findParent(&$array,$whos_your_daddy=0,$baby=[]){ // make $array modifiable foreach($array as $i=>&$row){ // make $row modifiable if($whos_your_daddy){ if($row['id']==$whos_your_daddy){ // found parent $row['nodes'][]=$baby; }elseif(isset($row['nodes'])){ // go down rabbit hole looking for parent findParent($row['nodes'],$whos_your_daddy,$baby); } }elseif($row['parent_id']){ // 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_id'],$row); } } return $array; } // $db->query('SELECT id,name_a AS name,parent_id FROM accounts_tree ORDER BY id'); // for($resultset=[]; $row=$res->fetch_assoc(); $resultset[]=$row); // inspired by: http://php.net/manual/en/mysqli-result.fetch-assoc.php#112924 $resultset=[ ['id'=>1,'name'=>'folder 1','parent_id'=>0], ['id'=>2,'name'=>'folder 2','parent_id'=>0], ['id'=>3,'name'=>'sub 1-1','parent_id'=>1], ['id'=>4,'name'=>'sub 2-1','parent_id'=>2], ['id'=>5,'name'=>'Sub 1-1-1','parent_id'=>3], ['id'=>6,'name'=>'folder 3','parent_id'=>0], ['id'=>7,'name'=>'sub 1-1-1-1','parent_id'=>5] ]; print_r(findParent($resultset));
Output for git.master_jit, git.master, rfc.property-hooks
Array ( [0] => Array ( [id] => 1 [name] => folder 1 [parent_id] => 0 [nodes] => Array ( [0] => Array ( [id] => 3 [name] => sub 1-1 [parent_id] => 1 [nodes] => Array ( [0] => Array ( [id] => 5 [name] => Sub 1-1-1 [parent_id] => 3 [nodes] => Array ( [0] => Array ( [id] => 7 [name] => sub 1-1-1-1 [parent_id] => 5 ) ) ) ) ) ) ) [1] => Array ( [id] => 2 [name] => folder 2 [parent_id] => 0 [nodes] => Array ( [0] => Array ( [id] => 4 [name] => sub 2-1 [parent_id] => 2 ) ) ) [5] => Array ( [id] => 6 [name] => folder 3 [parent_id] => 0 ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
56.06 ms | 405 KiB | 8 Q