3v4l.org

run code in 300+ PHP versions simultaneously
<?php header('Content-Type: application/json; charset="utf-8"'); /** * Helper function * * @param array $d flat data, implementing a id/parent id (adjacency list) structure * @param mixed $r root id, node to return * @param string $pk parent id index * @param string $k id index * @param string $c children index * @return array */ function makeRecursive($d, $r = 0, $pk = 'parent', $k = 'id', $c = 'children') { $m = array(); foreach ($d as $e) { isset($m[$e[$pk]]) ?: $m[$e[$pk]] = array(); isset($m[$e[$k]]) ?: $m[$e[$k]] = array(); $m[$e[$pk]][] = array_merge($e, array($c => &$m[$e[$k]])); } return $m[$r][0]; // remove [0] if there could be more than one root nodes } echo json_encode(makeRecursive(array( array('id' => 5273, 'parent' => 0, 'name' => 'John Doe'), array('id' => 6032, 'parent' => 5273, 'name' => 'Sally Smith'), array('id' => 6034, 'parent' => 6032, 'name' => 'Mike Jones'), array('id' => 6035, 'parent' => 6034, 'name' => 'Jason Williams'), array('id' => 6036, 'parent' => 5273, 'name' => 'Sara Johnson'), array('id' => 6037, 'parent' => 5273, 'name' => 'Dave Wilson'), array('id' => 6038, 'parent' => 6037, 'name' => 'Amy Martin'), )));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/s2PNC
function name:  (null)
number of ops:  11
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   INIT_FCALL                                               'header'
          1        SEND_VAL                                                 'Content-Type%3A+application%2Fjson%3B+charset%3D%22utf-8%22'
          2        DO_ICALL                                                 
   25     3        INIT_FCALL                                               'json_encode'
          4        INIT_FCALL                                               'makerecursive'
   26     5        SEND_VAL                                                 <array>
   25     6        DO_FCALL                                      0  $1      
   26     7        SEND_VAR                                                 $1
   25     8        DO_ICALL                                         $2      
   26     9        ECHO                                                     $2
   33    10      > RETURN                                                   1

Function makerecursive:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 36
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 36
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 36
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 36
filename:       /in/s2PNC
function name:  makeRecursive
number of ops:  41
compiled vars:  !0 = $d, !1 = $r, !2 = $pk, !3 = $k, !4 = $c, !5 = $m, !6 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      0
          2        RECV_INIT                                        !2      'parent'
          3        RECV_INIT                                        !3      'id'
          4        RECV_INIT                                        !4      'children'
   15     5        ASSIGN                                                   !5, <array>
   16     6      > FE_RESET_R                                       $8      !0, ->36
          7    > > FE_FETCH_R                                               $8, !6, ->36
   17     8    >   FETCH_DIM_R                                      ~9      !6, !2
          9        ISSET_ISEMPTY_DIM_OBJ                         0  ~10     !5, ~9
         10        JMP_SET                                          ~11     ~10, ->15
         11        FETCH_DIM_R                                      ~12     !6, !2
         12        ASSIGN_DIM                                       ~13     !5, ~12
         13        OP_DATA                                                  <array>
         14        QM_ASSIGN                                        ~11     ~13
         15        FREE                                                     ~11
   18    16        FETCH_DIM_R                                      ~14     !6, !3
         17        ISSET_ISEMPTY_DIM_OBJ                         0  ~15     !5, ~14
         18        JMP_SET                                          ~16     ~15, ->23
         19        FETCH_DIM_R                                      ~17     !6, !3
         20        ASSIGN_DIM                                       ~18     !5, ~17
         21        OP_DATA                                                  <array>
         22        QM_ASSIGN                                        ~16     ~18
         23        FREE                                                     ~16
   19    24        FETCH_DIM_R                                      ~19     !6, !2
         25        INIT_FCALL                                               'array_merge'
         26        SEND_VAR                                                 !6
         27        FETCH_DIM_R                                      ~22     !6, !3
         28        FETCH_DIM_W                                      $23     !5, ~22
         29        INIT_ARRAY                                       ~24     $23, !4
         30        SEND_VAL                                                 ~24
         31        DO_ICALL                                         $25     
         32        FETCH_DIM_W                                      $20     !5, ~19
         33        ASSIGN_DIM                                               $20
         34        OP_DATA                                                  $25
   16    35      > JMP                                                      ->7
         36    >   FE_FREE                                                  $8
   22    37        FETCH_DIM_R                                      ~26     !5, !1
         38        FETCH_DIM_R                                      ~27     ~26, 0
         39      > RETURN                                                   ~27
   23    40*     > RETURN                                                   null

End of function makerecursive

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
175.14 ms | 1454 KiB | 17 Q