3v4l.org

run code in 300+ PHP versions simultaneously
<?php function treeifyDbRows(array $inputArray, array $branchRules): array { $output = []; if ($branchRules) { $ruleSet = array_shift($branchRules); $masterRule = $ruleSet[0]; $branchChildren = []; foreach ($inputArray as $index => $element) { $branchChildren[] = $element; //coalesce null to 0 if (($inputArray[$index + 1][$masterRule] ?? 0) !== $element[$masterRule]) { $branch = []; foreach ($ruleSet as $rule) { $branch[$rule] = $branchChildren[0][$rule]; } $children = treeifyDbRows($branchChildren, $branchRules); if ($children) { $branch['children'] = $children; } $output[] = $branch; $branchChildren = []; } } } return $output; } $data = [ [ "countryID" => 1, "countryName" => "USA", "regionID" => 10, "regionName" => "Indiana", "cityID" => 100, "cityName"=> "Indianapolis", ], [ "countryID" => 1, "countryName" => "USA", "regionID" => 10, "regionName" => "Indiana", "cityID" => 101, "cityName"=> "Bloomington", ], [ "countryID" => 1, "countryName" => "USA", "regionID" => 11, "regionName" => "Michigan", "cityID" => 102, "cityName"=> "Lansing", ], [ "countryID" => 2, "countryName" => "Canada", "regionID" => 12, "regionName" => "Ontario", "cityID" => 103, "cityName"=> "Toronto", ], [ "countryID" => 2, "countryName" => "Canada", "regionID" => 12, "regionName" => "Ontario", "cityID" => 104, "cityName"=> "Ottawa", ], [ "countryID" => 2, "countryName" => "Canada", "regionID" => 12, "regionName" => "Ontario", "cityID" => 105, "cityName"=> "Windsor", ], [ "countryID" => 2, "countryName" => "Canada", "regionID" => 25, "regionName" => "Quebec", "cityID" => 106, "cityName"=> "Montreal", ], ]; $rules = [ ["countryID", "countryName"], ["regionID", "regionName"], ["cityID", "cityName"], ]; $results = treeifyDbRows($data, $rules); var_dump($results);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/ejtuZ
function name:  (null)
number of ops:  11
compiled vars:  !0 = $data, !1 = $rules, !2 = $results
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   29     0  E >   ASSIGN                                                   !0, <array>
   89     1        ASSIGN                                                   !1, <array>
   95     2        INIT_FCALL                                               'treeifydbrows'
          3        SEND_VAR                                                 !0
          4        SEND_VAR                                                 !1
          5        DO_FCALL                                      0  $5      
          6        ASSIGN                                                   !2, $5
   96     7        INIT_FCALL                                               'var_dump'
          8        SEND_VAR                                                 !2
          9        DO_ICALL                                                 
         10      > RETURN                                                   1

Function treeifydbrows:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 4, Position 2 = 46
Branch analysis from position: 4
2 jumps found. (Code = 77) Position 1 = 12, Position 2 = 45
Branch analysis from position: 12
2 jumps found. (Code = 78) Position 1 = 13, Position 2 = 45
Branch analysis from position: 13
2 jumps found. (Code = 43) Position 1 = 24, Position 2 = 44
Branch analysis from position: 24
2 jumps found. (Code = 77) Position 1 = 26, Position 2 = 32
Branch analysis from position: 26
2 jumps found. (Code = 78) Position 1 = 27, Position 2 = 32
Branch analysis from position: 27
1 jumps found. (Code = 42) Position 1 = 26
Branch analysis from position: 26
Branch analysis from position: 32
2 jumps found. (Code = 43) Position 1 = 39, Position 2 = 41
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
Branch analysis from position: 41
Branch analysis from position: 32
Branch analysis from position: 44
Branch analysis from position: 45
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 45
Branch analysis from position: 46
filename:       /in/ejtuZ
function name:  treeifyDbRows
number of ops:  50
compiled vars:  !0 = $inputArray, !1 = $branchRules, !2 = $output, !3 = $ruleSet, !4 = $masterRule, !5 = $branchChildren, !6 = $element, !7 = $index, !8 = $branch, !9 = $rule, !10 = $children
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        ASSIGN                                                   !2, <array>
    5     3      > JMPZ                                                     !1, ->46
    6     4    >   INIT_FCALL                                               'array_shift'
          5        SEND_REF                                                 !1
          6        DO_ICALL                                         $12     
          7        ASSIGN                                                   !3, $12
    7     8        FETCH_DIM_R                                      ~14     !3, 0
          9        ASSIGN                                                   !4, ~14
    8    10        ASSIGN                                                   !5, <array>
    9    11      > FE_RESET_R                                       $17     !0, ->45
         12    > > FE_FETCH_R                                       ~18     $17, !6, ->45
         13    >   ASSIGN                                                   !7, ~18
   10    14        ASSIGN_DIM                                               !5
         15        OP_DATA                                                  !6
   12    16        ADD                                              ~21     !7, 1
         17        FETCH_DIM_IS                                     ~22     !0, ~21
         18        FETCH_DIM_IS                                     ~23     ~22, !4
         19        COALESCE                                         ~24     ~23
         20        QM_ASSIGN                                        ~24     0
         21        FETCH_DIM_R                                      ~25     !6, !4
         22        IS_NOT_IDENTICAL                                         ~24, ~25
         23      > JMPZ                                                     ~26, ->44
   13    24    >   ASSIGN                                                   !8, <array>
   14    25      > FE_RESET_R                                       $28     !3, ->32
         26    > > FE_FETCH_R                                               $28, !9, ->32
   15    27    >   FETCH_DIM_R                                      ~30     !5, 0
         28        FETCH_DIM_R                                      ~31     ~30, !9
         29        ASSIGN_DIM                                               !8, !9
         30        OP_DATA                                                  ~31
   14    31      > JMP                                                      ->26
         32    >   FE_FREE                                                  $28
   17    33        INIT_FCALL_BY_NAME                                       'treeifyDbRows'
         34        SEND_VAR_EX                                              !5
         35        SEND_VAR_EX                                              !1
         36        DO_FCALL                                      0  $32     
         37        ASSIGN                                                   !10, $32
   18    38      > JMPZ                                                     !10, ->41
   19    39    >   ASSIGN_DIM                                               !8, 'children'
         40        OP_DATA                                                  !10
   21    41    >   ASSIGN_DIM                                               !2
         42        OP_DATA                                                  !8
   22    43        ASSIGN                                                   !5, <array>
    9    44    > > JMP                                                      ->12
         45    >   FE_FREE                                                  $17
   27    46    >   VERIFY_RETURN_TYPE                                       !2
         47      > RETURN                                                   !2
   28    48*       VERIFY_RETURN_TYPE                                       
         49*     > RETURN                                                   null

End of function treeifydbrows

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
178.37 ms | 1407 KiB | 18 Q