3v4l.org

run code in 300+ PHP versions simultaneously
<?php $arr = [ ['comment_id' => 32, 'article_id' => 5, 'parent_id' => 31, 'user_id' => 22, 'content' => 'dolor sit amet consectetur adipisicing', 'created_at' => '02-04-2021'], ['comment_id' => 30, 'article_id' => 5, 'parent_id' => 29, 'user_id' => 22, 'content' => 'Lorem ipsum dolor sit amet.', 'created_at' => '01-04-2021'], ['comment_id' => 31, 'article_id' => 5, 'parent_id' => 29, 'user_id' => 22, 'content' => 'Sit amet consectetur adipisicing', 'created_at' => '02-04-2021'], ['comment_id' => 29, 'article_id' => 5, 'parent_id' => 0, 'user_id' => 22, 'content' => 'dolor sit amet consectetur adipisicing', 'created_at' => '01-04-2021'] ]; $arr_by_comment_id = array_column($arr, null, 'comment_id'); $root_entry = null; foreach ($arr_by_comment_id as &$entry) { if ($entry['parent_id'] === 0) { $root_entry =& $entry; } elseif (array_key_exists($entry['parent_id'], $arr_by_comment_id)) { $arr_by_comment_id[$entry['parent_id']]['comments'][] = $entry; } } unset($entry); // For safety (after reference looping) print_r($root_entry);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [comment_id] => 29 [article_id] => 5 [parent_id] => 0 [user_id] => 22 [content] => dolor sit amet consectetur adipisicing [created_at] => 01-04-2021 [comments] => Array ( [0] => Array ( [comment_id] => 30 [article_id] => 5 [parent_id] => 29 [user_id] => 22 [content] => Lorem ipsum dolor sit amet. [created_at] => 01-04-2021 ) [1] => Array ( [comment_id] => 31 [article_id] => 5 [parent_id] => 29 [user_id] => 22 [content] => Sit amet consectetur adipisicing [created_at] => 02-04-2021 [comments] => Array ( [0] => Array ( [comment_id] => 32 [article_id] => 5 [parent_id] => 31 [user_id] => 22 [content] => dolor sit amet consectetur adipisicing [created_at] => 02-04-2021 ) ) ) ) )

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:
30.98 ms | 409 KiB | 5 Q