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);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 9, Position 2 = 24
Branch analysis from position: 9
2 jumps found. (Code = 126) Position 1 = 10, Position 2 = 24
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 15
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 23
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 23
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/gSkH2
function name:  (null)
number of ops:  30
compiled vars:  !0 = $arr, !1 = $arr_by_comment_id, !2 = $root_entry, !3 = $entry
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   10     1        INIT_FCALL                                               'array_column'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 null
          4        SEND_VAL                                                 'comment_id'
          5        DO_ICALL                                         $5      
          6        ASSIGN                                                   !1, $5
   12     7        ASSIGN                                                   !2, null
   13     8      > FE_RESET_RW                                      $8      !1, ->24
          9    > > FE_FETCH_RW                                              $8, !3, ->24
   14    10    >   FETCH_DIM_R                                      ~9      !3, 'parent_id'
         11        IS_IDENTICAL                                             ~9, 0
         12      > JMPZ                                                     ~10, ->15
   15    13    >   ASSIGN_REF                                               !2, !3
   14    14      > JMP                                                      ->23
   16    15    >   FETCH_DIM_R                                      ~12     !3, 'parent_id'
         16        ARRAY_KEY_EXISTS                                         ~12, !1
         17      > JMPZ                                                     ~13, ->23
   17    18    >   FETCH_DIM_R                                      ~14     !3, 'parent_id'
         19        FETCH_DIM_W                                      $15     !1, ~14
         20        FETCH_DIM_W                                      $16     $15, 'comments'
         21        ASSIGN_DIM                                               $16
         22        OP_DATA                                                  !3
   13    23    > > JMP                                                      ->9
         24    >   FE_FREE                                                  $8
   20    25        UNSET_CV                                                 !3
   22    26        INIT_FCALL                                               'print_r'
         27        SEND_VAR                                                 !2
         28        DO_ICALL                                                 
         29      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
255 ms | 1002 KiB | 15 Q