3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Normalise a flat dot notation array. * * @param array $rows The flat array * @param string $dot The character that separates the layers. * * @return array */ function unflatten(array $rows, string $dot = "."): array { # The last row's main table columns $last_main_table = []; # A collection of all the joined tables belonging to a given row of the main table $joined_tables = []; # For each row in the result set foreach($rows as $id => $row){ # Contains this row's main table columns $main_table = []; # Contains this row's joined table columns $joined_table = []; # Foreach column and value foreach($row as $col => $val){ /** * Whether a column name contains a period or not * is the determining factor of whether the column * belongs to the main table (no period) or a joined * table (periods). */ if(!strpos($col, $dot)){ //if the column belongs to the main table $main_table[$col] = $val; } else { //If the column belongs to a joined table # Break open the column name $col = explode($dot,$col); # Extract out the joined table, and the column names (that may include joined children) $joined_table[array_shift($col)][$id][implode($dot,$col)] = $val; } } # If this is the first row, or if this row's main columns are the same as the last row if(!$last_main_table || ($main_table == $last_main_table)){ # Update the last main table variable $last_main_table = $main_table; # merge all rows of columns belonging to joined tables $joined_tables = array_merge_recursive($joined_tables ?:[], $joined_table ?:[]); # Go to the next row continue; } # At this point, the main columns are different from the previous row's main columns # Add the last row's main columns, merged with all of it's joined table rows to the normalised array $normalised[] = array_merge($last_main_table, $joined_tables); # Update the last main table variable $last_main_table = $main_table; # Reset the joined tables $joined_tables = []; # Add this row's joined table columns $joined_tables = array_merge_recursive($joined_tables ?:[], $joined_table ?:[]); } # Capture the last row $normalised[] = array_merge($last_main_table, $joined_tables); # Go deeper foreach($normalised as $id => $row){ //For each row that is now normalised foreach($row as $key => $val){ //For each column value if(is_array($val)){ //if any of the values are arrays, make sure that array is also normalised $normalised[$id][$key] = unflatten($val, $dot); } } } return $normalised; } $results[] = [ 'id' => 'A1', 'address.id' => '11f456A1', 'address.coordinates.lat' => '12.345', 'address.coordinates.lng' => '67.89', 'address.coordinates.geo.accurate' => true, ]; $results[] = [ 'id' => 'A1', 'address.id' => '22f456A1', 'address.coordinates.lat' => '12.345', 'address.coordinates.lng' => '67.89', 'address.coordinates.geo.accurate' => true, ]; $results[] = [ 'id' => 'A2', 'address.id' => '44f456A2', 'address.coordinates.lat' => '11.345', 'address.coordinates.lng' => '67.89', 'address.coordinates.geo.accurate' => true, ]; $results[] = [ 'id' => 'A2', 'address.id' => '44f456A2', 'address.coordinates.lat' => '22.345', 'address.coordinates.lng' => '67.89', 'address.coordinates.geo.accurate' => true, ]; $normalised = unflatten($results); var_export($normalised);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/P4RVZ
function name:  (null)
number of ops:  16
compiled vars:  !0 = $results, !1 = $normalised
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   98     0  E >   ASSIGN_DIM                                               !0
   99     1        OP_DATA                                                  <array>
  105     2        ASSIGN_DIM                                               !0
  106     3        OP_DATA                                                  <array>
  112     4        ASSIGN_DIM                                               !0
  113     5        OP_DATA                                                  <array>
  119     6        ASSIGN_DIM                                               !0
  120     7        OP_DATA                                                  <array>
  127     8        INIT_FCALL                                               'unflatten'
          9        SEND_VAR                                                 !0
         10        DO_FCALL                                      0  $6      
         11        ASSIGN                                                   !1, $6
  128    12        INIT_FCALL                                               'var_export'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                                 
         15      > RETURN                                                   1

Function unflatten:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 5, Position 2 = 73
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 73
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 38
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 38
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 38
2 jumps found. (Code = 47) Position 1 = 41, Position 2 = 43
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 44, Position 2 = 55
Branch analysis from position: 44
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 55
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 43
Branch analysis from position: 38
Branch analysis from position: 73
2 jumps found. (Code = 77) Position 1 = 81, Position 2 = 98
Branch analysis from position: 81
2 jumps found. (Code = 78) Position 1 = 82, Position 2 = 98
Branch analysis from position: 82
2 jumps found. (Code = 77) Position 1 = 84, Position 2 = 96
Branch analysis from position: 84
2 jumps found. (Code = 78) Position 1 = 85, Position 2 = 96
Branch analysis from position: 85
2 jumps found. (Code = 43) Position 1 = 88, Position 2 = 95
Branch analysis from position: 88
1 jumps found. (Code = 42) Position 1 = 84
Branch analysis from position: 84
Branch analysis from position: 95
Branch analysis from position: 96
1 jumps found. (Code = 42) Position 1 = 81
Branch analysis from position: 81
Branch analysis from position: 96
Branch analysis from position: 98
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 98
Branch analysis from position: 73
filename:       /in/P4RVZ
function name:  unflatten
number of ops:  103
compiled vars:  !0 = $rows, !1 = $dot, !2 = $last_main_table, !3 = $joined_tables, !4 = $row, !5 = $id, !6 = $main_table, !7 = $joined_table, !8 = $val, !9 = $col, !10 = $normalised, !11 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      '.'
   14     2        ASSIGN                                                   !2, <array>
   17     3        ASSIGN                                                   !3, <array>
   20     4      > FE_RESET_R                                       $14     !0, ->73
          5    > > FE_FETCH_R                                       ~15     $14, !4, ->73
          6    >   ASSIGN                                                   !5, ~15
   23     7        ASSIGN                                                   !6, <array>
   26     8        ASSIGN                                                   !7, <array>
   29     9      > FE_RESET_R                                       $19     !4, ->38
         10    > > FE_FETCH_R                                       ~20     $19, !8, ->38
         11    >   ASSIGN                                                   !9, ~20
   37    12        INIT_FCALL                                               'strpos'
         13        SEND_VAR                                                 !9
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                         $22     
         16        BOOL_NOT                                         ~23     $22
         17      > JMPZ                                                     ~23, ->21
   39    18    >   ASSIGN_DIM                                               !6, !9
         19        OP_DATA                                                  !8
   37    20      > JMP                                                      ->37
   45    21    >   INIT_FCALL                                               'explode'
         22        SEND_VAR                                                 !1
         23        SEND_VAR                                                 !9
         24        DO_ICALL                                         $25     
         25        ASSIGN                                                   !9, $25
   48    26        INIT_FCALL                                               'array_shift'
         27        SEND_REF                                                 !9
         28        DO_ICALL                                         $27     
         29        INIT_FCALL                                               'implode'
         30        SEND_VAR                                                 !1
         31        SEND_VAR                                                 !9
         32        DO_ICALL                                         $30     
         33        FETCH_DIM_W                                      $28     !7, $27
         34        FETCH_DIM_W                                      $29     $28, !5
         35        ASSIGN_DIM                                               $29, $30
         36        OP_DATA                                                  !8
   29    37    > > JMP                                                      ->10
         38    >   FE_FREE                                                  $19
   53    39        BOOL_NOT                                         ~32     !2
         40      > JMPNZ_EX                                         ~32     ~32, ->43
         41    >   IS_EQUAL                                         ~33     !6, !2
         42        BOOL                                             ~32     ~33
         43    > > JMPZ                                                     ~32, ->55
   56    44    >   ASSIGN                                                   !2, !6
   59    45        INIT_FCALL                                               'array_merge_recursive'
         46        JMP_SET                                          ~35     !3, ->48
         47        QM_ASSIGN                                        ~35     <array>
         48        SEND_VAL                                                 ~35
         49        JMP_SET                                          ~36     !7, ->51
         50        QM_ASSIGN                                        ~36     <array>
         51        SEND_VAL                                                 ~36
         52        DO_ICALL                                         $37     
         53        ASSIGN                                                   !3, $37
   62    54      > JMP                                                      ->5
   68    55    >   INIT_FCALL                                               'array_merge'
         56        SEND_VAR                                                 !2
         57        SEND_VAR                                                 !3
         58        DO_ICALL                                         $40     
         59        ASSIGN_DIM                                               !10
         60        OP_DATA                                                  $40
   71    61        ASSIGN                                                   !2, !6
   74    62        ASSIGN                                                   !3, <array>
   77    63        INIT_FCALL                                               'array_merge_recursive'
         64        JMP_SET                                          ~43     !3, ->66
         65        QM_ASSIGN                                        ~43     <array>
         66        SEND_VAL                                                 ~43
         67        JMP_SET                                          ~44     !7, ->69
         68        QM_ASSIGN                                        ~44     <array>
         69        SEND_VAL                                                 ~44
         70        DO_ICALL                                         $45     
         71        ASSIGN                                                   !3, $45
   20    72      > JMP                                                      ->5
         73    >   FE_FREE                                                  $14
   81    74        INIT_FCALL                                               'array_merge'
         75        SEND_VAR                                                 !2
         76        SEND_VAR                                                 !3
         77        DO_ICALL                                         $48     
         78        ASSIGN_DIM                                               !10
         79        OP_DATA                                                  $48
   84    80      > FE_RESET_R                                       $49     !10, ->98
         81    > > FE_FETCH_R                                       ~50     $49, !4, ->98
         82    >   ASSIGN                                                   !5, ~50
   86    83      > FE_RESET_R                                       $52     !4, ->96
         84    > > FE_FETCH_R                                       ~53     $52, !8, ->96
         85    >   ASSIGN                                                   !11, ~53
   88    86        TYPE_CHECK                                  128          !8
         87      > JMPZ                                                     ~55, ->95
   90    88    >   INIT_FCALL_BY_NAME                                       'unflatten'
         89        SEND_VAR_EX                                              !8
         90        SEND_VAR_EX                                              !1
         91        DO_FCALL                                      0  $58     
         92        FETCH_DIM_W                                      $56     !10, !5
         93        ASSIGN_DIM                                               $56, !11
         94        OP_DATA                                                  $58
   86    95    > > JMP                                                      ->84
         96    >   FE_FREE                                                  $52
   84    97      > JMP                                                      ->81
         98    >   FE_FREE                                                  $49
   95    99        VERIFY_RETURN_TYPE                                       !10
        100      > RETURN                                                   !10
   96   101*       VERIFY_RETURN_TYPE                                       
        102*     > RETURN                                                   null

End of function unflatten

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
149.01 ms | 1027 KiB | 21 Q