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);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 2, Position 2 = 13
Branch analysis from position: 2
2 jumps found. (Code = 126) Position 1 = 3, Position 2 = 13
Branch analysis from position: 3
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 12
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 2
Branch analysis from position: 2
Branch analysis from position: 12
Branch analysis from position: 13
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 13
filename:       /in/4JuIa
function name:  (null)
number of ops:  19
compiled vars:  !0 = $comments, !1 = $value, !2 = $id, !3 = $parentId
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   12     1      > FE_RESET_RW                                      $5      !0, ->13
          2    > > FE_FETCH_RW                                      ~6      $5, !1, ->13
          3    >   ASSIGN                                                   !2, ~6
   15     4        FETCH_DIM_R                                      ~8      !1, 'parent'
          5        ASSIGN                                           ~9      !3, ~8
          6      > JMPZ                                                     ~9, ->12
   17     7    >   FETCH_DIM_W                                      $10     !0, !3
          8        FETCH_DIM_W                                      $11     $10, 0
          9        FETCH_DIM_W                                      $12     $11, !2
         10        ASSIGN_REF                                               $12, !1
   18    11        UNSET_DIM                                                !0, !2
   12    12    > > JMP                                                      ->2
         13    >   FE_FREE                                                  $5
   21    14        UNSET_CV                                                 !1
   22    15        INIT_FCALL                                               'print_r'
         16        SEND_VAR                                                 !0
         17        DO_ICALL                                                 
   31    18      > RETURN                                                   1

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
183.38 ms | 1386 KiB | 15 Q