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 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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 ) ) ) )
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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 ) ) ) )
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.5 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined index: 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 ) ) ) )
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28, 7.3.0 - 7.3.4, 7.3.32 - 7.3.33
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 ) ) ) )

preferences:
191.39 ms | 404 KiB | 259 Q