3v4l.org

run code in 300+ PHP versions simultaneously
<?php $comments = [ 1 => ['content' => "The first comment", 'parent' => 0], 2 => ['content' => "The second comment", 'parent' => 0], 3 => ['content' => "Reply of first comment", 'parent' => 1], 4 => ['content' => "Another reply of first comemnt", 'parent' => 1], 5 => ['content' => "Reply of a reply of the first comment", 'parent' => 4] ]; foreach($comments as $id => &$value) { # check if there is a parent if ($parentId = $value['parent']) { $comments[$parentId][0][$id] =& $value; # add child to parent unset($comments[$id]); # remove reference from topmost level } } unset($value); # remove iterator reference print_r($comments); # your tree // foreach($comments as $id => &$c) { // if (isset($comments[$c['parent']])) { // $comments[$c['parent']]['comments'][$id] =& $c; // unset($comments[$id]); // } // } // var_dump($comments);
Output for git.master, git.master_jit, rfc.property-hooks
Warning: Undefined array key "parent" in /in/4JuIa on line 15 Array ( [1] => Array ( [content] => The first comment [parent] => 0 [0] => Array ( [3] => Array ( [content] => Reply of first comment [parent] => 1 ) [4] => Array ( [content] => Another reply of first comemnt [parent] => 1 ) ) ) [2] => Array ( [content] => The second comment [parent] => 0 ) [4] => Array ( [0] => Array ( [5] => Array ( [content] => Reply of a reply of the first comment [parent] => 4 ) ) ) )

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:
60.68 ms | 403 KiB | 8 Q