3v4l.org

run code in 500+ 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 = 67
Branch analysis from position: 5
2 jumps found. (Code = 78) Position 1 = 6, Position 2 = 67
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 10, Position 2 = 32
Branch analysis from position: 10
2 jumps found. (Code = 78) Position 1 = 11, Position 2 = 32
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 15, Position 2 = 18
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 31
Branch analysis from position: 31
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 18
1 jumps found. (Code = 42) Position 1 = 10
Branch analysis from position: 10
Branch analysis from position: 32
2 jumps found. (Code = 47) Position 1 = 35, Position 2 = 37
Branch analysis from position: 35
2 jumps found. (Code = 43) Position 1 = 38, Position 2 = 49
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 5
Branch analysis from position: 5
Branch analysis from position: 37
Branch analysis from position: 32
Branch analysis from position: 67
2 jumps found. (Code = 77) Position 1 = 75, Position 2 = 92
Branch analysis from position: 75
2 jumps found. (Code = 78) Position 1 = 76, Position 2 = 92
Branch analysis from position: 76
2 jumps found. (Code = 77) Position 1 = 78, Position 2 = 90
Branch analysis from position: 78
2 jumps found. (Code = 78) Position 1 = 79, Position 2 = 90
Branch analysis from position: 79
2 jumps found. (Code = 43) Position 1 = 82, Position 2 = 89
Branch analysis from position: 82
1 jumps found. (Code = 42) Position 1 = 78
Branch analysis from position: 78
Branch analysis from position: 89
Branch analysis from position: 90
1 jumps found. (Code = 42) Position 1 = 75
Branch analysis from position: 75
Branch analysis from position: 90
Branch analysis from position: 92
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 92
Branch analysis from position: 67
filename:       /in/P4RVZ
function name:  unflatten
number of ops:  97
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, ->67
          5    > > FE_FETCH_R                                           ~15     $14, !4, ->67
          6    >   ASSIGN                                                       !5, ~15
   23     7        ASSIGN                                                       !6, <array>
   26     8        ASSIGN                                                       !7, <array>
   29     9      > FE_RESET_R                                           $19     !4, ->32
         10    > > FE_FETCH_R                                           ~20     $19, !8, ->32
         11    >   ASSIGN                                                       !9, ~20
   37    12        FRAMELESS_ICALL_2                strpos              ~22     !9, !1
         13        BOOL_NOT                                             ~23     ~22
         14      > JMPZ                                                         ~23, ->18
   39    15    >   ASSIGN_DIM                                                   !6, !9
         16        OP_DATA                                                      !8
   37    17      > JMP                                                          ->31
   45    18    >   INIT_FCALL                                                   'explode'
         19        SEND_VAR                                                     !1
         20        SEND_VAR                                                     !9
         21        DO_ICALL                                             $25     
         22        ASSIGN                                                       !9, $25
   48    23        INIT_FCALL                                                   'array_shift'
         24        SEND_REF                                                     !9
         25        DO_ICALL                                             $27     
         26        FRAMELESS_ICALL_2                implode             ~30     !1, !9
         27        FETCH_DIM_W                                          $28     !7, $27
         28        FETCH_DIM_W                                          $29     $28, !5
         29        ASSIGN_DIM                                                   $29, ~30
         30        OP_DATA                                                      !8
   29    31    > > JMP                                                          ->10
         32    >   FE_FREE                                                      $19
   53    33        BOOL_NOT                                             ~32     !2
         34      > JMPNZ_EX                                             ~32     ~32, ->37
         35    >   IS_EQUAL                                             ~33     !6, !2
         36        BOOL                                                 ~32     ~33
         37    > > JMPZ                                                         ~32, ->49
   56    38    >   ASSIGN                                                       !2, !6
   59    39        INIT_FCALL                                                   'array_merge_recursive'
         40        JMP_SET                                              ~35     !3, ->42
         41        QM_ASSIGN                                            ~35     <array>
         42        SEND_VAL                                                     ~35
         43        JMP_SET                                              ~36     !7, ->45
         44        QM_ASSIGN                                            ~36     <array>
         45        SEND_VAL                                                     ~36
         46        DO_ICALL                                             $37     
         47        ASSIGN                                                       !3, $37
   62    48      > JMP                                                          ->5
   68    49    >   INIT_FCALL                                                   'array_merge'
         50        SEND_VAR                                                     !2
         51        SEND_VAR                                                     !3
         52        DO_ICALL                                             $40     
         53        ASSIGN_DIM                                                   !10
         54        OP_DATA                                                      $40
   71    55        ASSIGN                                                       !2, !6
   74    56        ASSIGN                                                       !3, <array>
   77    57        INIT_FCALL                                                   'array_merge_recursive'
         58        JMP_SET                                              ~43     !3, ->60
         59        QM_ASSIGN                                            ~43     <array>
         60        SEND_VAL                                                     ~43
         61        JMP_SET                                              ~44     !7, ->63
         62        QM_ASSIGN                                            ~44     <array>
         63        SEND_VAL                                                     ~44
         64        DO_ICALL                                             $45     
         65        ASSIGN                                                       !3, $45
   20    66      > JMP                                                          ->5
         67    >   FE_FREE                                                      $14
   81    68        INIT_FCALL                                                   'array_merge'
         69        SEND_VAR                                                     !2
         70        SEND_VAR                                                     !3
         71        DO_ICALL                                             $48     
         72        ASSIGN_DIM                                                   !10
         73        OP_DATA                                                      $48
   84    74      > FE_RESET_R                                           $49     !10, ->92
         75    > > FE_FETCH_R                                           ~50     $49, !4, ->92
         76    >   ASSIGN                                                       !5, ~50
   86    77      > FE_RESET_R                                           $52     !4, ->90
         78    > > FE_FETCH_R                                           ~53     $52, !8, ->90
         79    >   ASSIGN                                                       !11, ~53
   88    80        TYPE_CHECK                                      128          !8
         81      > JMPZ                                                         ~55, ->89
   90    82    >   INIT_FCALL_BY_NAME                                           'unflatten'
         83        SEND_VAR_EX                                                  !8
         84        SEND_VAR_EX                                                  !1
         85        DO_FCALL                                          0  $58     
         86        FETCH_DIM_W                                          $56     !10, !5
         87        ASSIGN_DIM                                                   $56, !11
         88        OP_DATA                                                      $58
   86    89    > > JMP                                                          ->78
         90    >   FE_FREE                                                      $52
   84    91      > JMP                                                          ->75
         92    >   FE_FREE                                                      $49
   95    93        VERIFY_RETURN_TYPE                                           !10
         94      > RETURN                                                       !10
   96    95*       VERIFY_RETURN_TYPE                                           
         96*     > RETURN                                                       null

End of function unflatten

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
176.75 ms | 2587 KiB | 19 Q