3v4l.org

run code in 500+ PHP versions simultaneously
<?php $array = [ [ 'id' => 196, 'name' => 'Abilități spirituale', 'parent' => 0, 'depth' => 0, 'score' => 0, 'sort' => null, ], [ 'id' => 395, 'name' => 'Date generale', 'parent' => 196, 'depth' => 0, 'score' => 0, 'sort' => 1, ], [ 'id' => 201, 'name' => 'Gândirea pozitivă', 'parent' => 197, 'depth' => 0, 'score' => 0, 'sort' => 1, ], [ 'id' => 202, 'name' => 'Satisfacția personală', 'parent' => 197, 'depth' => 0, 'score' => 0, 'sort' => 2, ], [ 'id' => 203, 'name' => 'Concordanța cu sine', 'parent' => 197, 'depth' => 0, 'score' => 0, 'sort' => 3, ], [ 'id' => 204, 'name' => 'Managementul calitativ al timpului liber', 'parent' => 197, 'depth' => 0, 'score' => 0, 'sort' => 4, ], [ 'id' => 198, 'name' => 'Armonia cu ceilalți', 'parent' => 196, 'depth' => 0, 'score' => 0, 'sort' => 3, ], [ 'id' => 199, 'name' => 'Sensul vieții și misiunea personală', 'parent' => 196, 'depth' => 0, 'score' => 0, 'sort' => 4, ], [ 'id' => 200, 'name' => 'Perspectiva de ansamblu asupra realității', 'parent' => 196, 'depth' => 0, 'score' => 0, 'sort' => 5, ], [ 'id' => 197, 'name' => 'Armonia cu sine', 'parent' => 196, 'depth' => 0, 'score' => 0, 'sort' => 2, ], ]; function getScore(int $id): int { return [ 196 => 0, 197 => 0, 198 => 54, 199 => 49, 200 => 49, 201 => 57, 202 => 38, 203 => 29, 204 => 21, 395 => 0, ][$id] ?? 0; } function buildTree(array &$rows, int $parentId = 0, int $depth = 0): array { $tree = []; foreach ($rows as &$row) { if ($row['parent'] !== $parentId) { continue; } $row['depth'] = $depth; $row['children'] = buildTree($rows, $row['id'], $depth + 1); foreach ($row['children'] as &$child) { $row['score'] += $child['score'] += getScore($child['id']); $child['depth'] = $depth + 1; } $tree[] = $row; } return $tree; } usort( $array, fn($a, $b) => $a['sort'] <=> $b['sort'] ); var_export( buildTree($array) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7R2lb
function name:  (null)
number of ops:  13
compiled vars:  !0 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                       !0, <array>
  120     1        INIT_FCALL                                                   'usort'
  121     2        SEND_REF                                                     !0
  122     3        DECLARE_LAMBDA_FUNCTION                              ~2      [0]
  123     4        SEND_VAL                                                     ~2
  120     5        DO_ICALL                                                     
  125     6        INIT_FCALL                                                   'var_export'
  126     7        INIT_FCALL                                                   'buildtree'
          8        SEND_REF                                                     !0
          9        DO_FCALL                                          0  $4      
         10        SEND_VAR                                                     $4
  125    11        DO_ICALL                                                     
  127    12      > RETURN                                                       1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7R2lb
function name:  {closure:/in/7R2lb:122}
number of ops:  7
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  122     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        FETCH_DIM_R                                          ~2      !0, 'sort'
          3        FETCH_DIM_R                                          ~3      !1, 'sort'
          4        SPACESHIP                                            ~4      ~2, ~3
          5      > RETURN                                                       ~4
  123     6*     > RETURN                                                       null

End of Dynamic Function 0

Function getscore:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/7R2lb
function name:  getScore
number of ops:  8
compiled vars:  !0 = $id
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   86     0  E >   RECV                                                 !0      
   99     1        FETCH_DIM_IS                                         ~1      <array>, !0
          2        COALESCE                                             ~2      ~1
          3        QM_ASSIGN                                            ~2      0
          4        VERIFY_RETURN_TYPE                                           ~2
          5      > RETURN                                                       ~2
  100     6*       VERIFY_RETURN_TYPE                                           
          7*     > RETURN                                                       null

End of function getscore

Function buildtree:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 125) Position 1 = 5, Position 2 = 41
Branch analysis from position: 5
2 jumps found. (Code = 126) Position 1 = 6, Position 2 = 41
Branch analysis from position: 6
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 10
Branch analysis from position: 9
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 10
2 jumps found. (Code = 125) Position 1 = 24, Position 2 = 37
Branch analysis from position: 24
2 jumps found. (Code = 126) Position 1 = 25, Position 2 = 37
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 37
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
filename:       /in/7R2lb
function name:  buildTree
number of ops:  46
compiled vars:  !0 = $rows, !1 = $parentId, !2 = $depth, !3 = $tree, !4 = $row, !5 = $child
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
  102     0  E >   RECV                                                 !0      
          1        RECV_INIT                                            !1      0
          2        RECV_INIT                                            !2      0
  104     3        ASSIGN                                                       !3, <array>
  105     4      > FE_RESET_RW                                          $7      !0, ->41
          5    > > FE_FETCH_RW                                                  $7, !4, ->41
  106     6    >   FETCH_DIM_R                                          ~8      !4, 'parent'
          7        IS_NOT_IDENTICAL                                             !1, ~8
          8      > JMPZ                                                         ~9, ->10
  107     9    > > JMP                                                          ->5
  109    10    >   ASSIGN_DIM                                                   !4, 'depth'
         11        OP_DATA                                                      !2
  110    12        INIT_FCALL_BY_NAME                                           'buildTree'
         13        SEND_VAR_EX                                                  !0
         14        CHECK_FUNC_ARG                                               
         15        FETCH_DIM_FUNC_ARG                                   $12     !4, 'id'
         16        SEND_FUNC_ARG                                                $12
         17        ADD                                                  ~13     !2, 1
         18        SEND_VAL_EX                                                  ~13
         19        DO_FCALL                                          0  $14     
         20        ASSIGN_DIM                                                   !4, 'children'
         21        OP_DATA                                                      $14
  111    22        FETCH_DIM_W                                          $15     !4, 'children'
         23      > FE_RESET_RW                                          $16     $15, ->37
         24    > > FE_FETCH_RW                                                  $16, !5, ->37
  112    25    >   INIT_FCALL                                                   'getscore'
         26        FETCH_DIM_R                                          ~19     !5, 'id'
         27        SEND_VAL                                                     ~19
         28        DO_FCALL                                          0  $20     
         29        ASSIGN_DIM_OP                    +=               1  ~18     !5, 'score'
         30        OP_DATA                                                      $20
         31        ASSIGN_DIM_OP                    +=               1          !4, 'score'
         32        OP_DATA                                                      ~18
  113    33        ADD                                                  ~22     !2, 1
         34        ASSIGN_DIM                                                   !5, 'depth'
         35        OP_DATA                                                      ~22
  111    36      > JMP                                                          ->24
         37    >   FE_FREE                                                      $16
  115    38        ASSIGN_DIM                                                   !3
         39        OP_DATA                                                      !4
  105    40      > JMP                                                          ->5
         41    >   FE_FREE                                                      $7
  117    42        VERIFY_RETURN_TYPE                                           !3
         43      > RETURN                                                       !3
  118    44*       VERIFY_RETURN_TYPE                                           
         45*     > RETURN                                                       null

End of function buildtree

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
162.49 ms | 2183 KiB | 16 Q