3v4l.org

run code in 300+ PHP versions simultaneously
<?php $person = [ 'id' => '123', 'name' => 'John Doe', 'relationship_status' => 'Married', 'work_profession' => 'Teacher', 'work_salary' => '1500', 'work_status' => 'Working', 'work_place' => 'Lorem City School', 'from_province' => 'Dolor Sit Province', 'from_city_name' => 'Ipsum City', 'from_city_address' => 'Some Rd. 1337' ]; function reShapeArray(array $arr): array { $return = []; foreach ($arr as $key => $value) { $firstUnderscore = strpos($key, '_'); if ($firstUnderscore === false) { // already at the lowest level, simply return as is $return[$key] = $value; } else { // there's an underscore, dig into this [$top, $sub] = explode('_', $key, 2); $return[$top] = array_merge_recursive($return[$top] ?? [], reShapeArray([$sub => $value])); } } return $return; } function mergeSingleElementArrays(array $arr): array { $return = []; foreach ($arr as $key => $value) { if (is_array($value) && count($value) === 1) { $subKey = array_key_first($value); $return["{$key}_{$subKey}"] = $value[$subKey]; } else { $return[$key] = $value; } } return $return; } $tests = [ 'deep array with single prefixed key' => [ ['a_b_c_d' => 'foo', 'a_b_e' => 'bar',], ['a_b' => ['c' => ['d' => 'foo'], 'e' => 'bar']], ], 'single prefixed key' => [['a_b' => 'foo'], ['a_b' => 'foo']], 'standard' => [ $person, [ 'id' => '123', 'name' => 'John Doe', 'relationship_status' => 'Married', 'work' => [ 'profession' => 'Teacher', 'salary' => '1500', 'status' => 'Working', 'place' => 'Lorem City School', ], 'from' => [ 'province' => 'Dolor Sit Province', 'city' => [ 'name' => 'Ipsum City', 'address' => 'Some Rd. 1337', ], ], ], ], ]; $errors = []; foreach ($tests as $name => [$input, $expected]) { $result = reShapeArray($input); $foo = mergeSingleElementArrays($result); if ($foo !== $expected) { $errors[] = "Test '$name' failed: got " . json_encode($foo) . ' vs expected ' . json_encode($expected); } } if ($errors) { var_dump($errors); } else { echo "All tests passed\n"; }
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 41
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 41
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 26, Position 2 = 40
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 40
Branch analysis from position: 41
2 jumps found. (Code = 43) Position 1 = 43, Position 2 = 47
Branch analysis from position: 43
1 jumps found. (Code = 42) Position 1 = 48
Branch analysis from position: 48
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 47
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
filename:       /in/ePil6
function name:  (null)
number of ops:  49
compiled vars:  !0 = $person, !1 = $tests, !2 = $errors, !3 = $input, !4 = $expected, !5 = $name, !6 = $result, !7 = $foo
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   54     1        INIT_ARRAY                                       ~9      <array>, 'deep+array+with+single+prefixed+key'
          2        ADD_ARRAY_ELEMENT                                ~9      <array>, 'single+prefixed+key'
   59     3        INIT_ARRAY                                       ~10     !0
   54     4        ADD_ARRAY_ELEMENT                                ~10     <array>
          5        ADD_ARRAY_ELEMENT                                ~9      ~10, 'standard'
   52     6        ASSIGN                                                   !1, ~9
   82     7        ASSIGN                                                   !2, <array>
   83     8      > FE_RESET_R                                       $13     !1, ->41
          9    > > FE_FETCH_R                                       ~19     $13, $14, ->41
         10    >   FETCH_LIST_R                                     $15     $14, 0
         11        ASSIGN                                                   !3, $15
         12        FETCH_LIST_R                                     $17     $14, 1
         13        ASSIGN                                                   !4, $17
         14        FREE                                                     $14
         15        ASSIGN                                                   !5, ~19
   84    16        INIT_FCALL                                               'reshapearray'
         17        SEND_VAR                                                 !3
         18        DO_FCALL                                      0  $21     
         19        ASSIGN                                                   !6, $21
   85    20        INIT_FCALL                                               'mergesingleelementarrays'
         21        SEND_VAR                                                 !6
         22        DO_FCALL                                      0  $23     
         23        ASSIGN                                                   !7, $23
   87    24        IS_NOT_IDENTICAL                                         !7, !4
         25      > JMPZ                                                     ~25, ->40
   88    26    >   ROPE_INIT                                     3  ~28     'Test+%27'
         27        ROPE_ADD                                      1  ~28     ~28, !5
         28        ROPE_END                                      2  ~27     ~28, '%27+failed%3A+got+'
         29        INIT_FCALL                                               'json_encode'
         30        SEND_VAR                                                 !7
         31        DO_ICALL                                         $30     
         32        CONCAT                                           ~31     ~27, $30
         33        CONCAT                                           ~32     ~31, '+vs+expected+'
         34        INIT_FCALL                                               'json_encode'
         35        SEND_VAR                                                 !4
         36        DO_ICALL                                         $33     
         37        CONCAT                                           ~34     ~32, $33
         38        ASSIGN_DIM                                               !2
         39        OP_DATA                                                  ~34
   83    40    > > JMP                                                      ->9
         41    >   FE_FREE                                                  $13
   91    42      > JMPZ                                                     !2, ->47
   92    43    >   INIT_FCALL                                               'var_dump'
         44        SEND_VAR                                                 !2
         45        DO_ICALL                                                 
   91    46      > JMP                                                      ->48
   94    47    >   ECHO                                                     'All+tests+passed%0A'
   95    48    > > RETURN                                                   1

Function reshapearray:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 39
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 39
Branch analysis from position: 4
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
filename:       /in/ePil6
function name:  reShapeArray
number of ops:  44
compiled vars:  !0 = $arr, !1 = $return, !2 = $value, !3 = $key, !4 = $firstUnderscore, !5 = $top, !6 = $sub
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
   18     1        ASSIGN                                                   !1, <array>
   20     2      > FE_RESET_R                                       $8      !0, ->39
          3    > > FE_FETCH_R                                       ~9      $8, !2, ->39
          4    >   ASSIGN                                                   !3, ~9
   21     5        INIT_FCALL                                               'strpos'
          6        SEND_VAR                                                 !3
          7        SEND_VAL                                                 '_'
          8        DO_ICALL                                         $11     
          9        ASSIGN                                                   !4, $11
   23    10        TYPE_CHECK                                    4          !4
         11      > JMPZ                                                     ~13, ->15
   25    12    >   ASSIGN_DIM                                               !1, !3
         13        OP_DATA                                                  !2
   23    14      > JMP                                                      ->38
   28    15    >   INIT_FCALL                                               'explode'
         16        SEND_VAL                                                 '_'
         17        SEND_VAR                                                 !3
         18        SEND_VAL                                                 2
         19        DO_ICALL                                         $15     
         20        FETCH_LIST_R                                     $16     $15, 0
         21        ASSIGN                                                   !5, $16
         22        FETCH_LIST_R                                     $18     $15, 1
         23        ASSIGN                                                   !6, $18
         24        FREE                                                     $15
   30    25        INIT_FCALL                                               'array_merge_recursive'
         26        FETCH_DIM_IS                                     ~21     !1, !5
         27        COALESCE                                         ~22     ~21
         28        QM_ASSIGN                                        ~22     <array>
         29        SEND_VAL                                                 ~22
         30        INIT_FCALL_BY_NAME                                       'reShapeArray'
         31        INIT_ARRAY                                       ~23     !2, !6
         32        SEND_VAL_EX                                              ~23
         33        DO_FCALL                                      0  $24     
         34        SEND_VAR                                                 $24
         35        DO_ICALL                                         $25     
         36        ASSIGN_DIM                                               !1, !5
         37        OP_DATA                                                  $25
   20    38    > > JMP                                                      ->3
         39    >   FE_FREE                                                  $8
   34    40        VERIFY_RETURN_TYPE                                       !1
         41      > RETURN                                                   !1
   35    42*       VERIFY_RETURN_TYPE                                       
         43*     > RETURN                                                   null

End of function reshapearray

Function mergesingleelementarrays:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 3, Position 2 = 25
Branch analysis from position: 3
2 jumps found. (Code = 78) Position 1 = 4, Position 2 = 25
Branch analysis from position: 4
2 jumps found. (Code = 46) Position 1 = 7, Position 2 = 10
Branch analysis from position: 7
2 jumps found. (Code = 43) Position 1 = 11, Position 2 = 22
Branch analysis from position: 11
1 jumps found. (Code = 42) Position 1 = 24
Branch analysis from position: 24
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 22
1 jumps found. (Code = 42) Position 1 = 3
Branch analysis from position: 3
Branch analysis from position: 10
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/ePil6
function name:  mergeSingleElementArrays
number of ops:  30
compiled vars:  !0 = $arr, !1 = $return, !2 = $value, !3 = $key, !4 = $subKey
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   RECV                                             !0      
   38     1        ASSIGN                                                   !1, <array>
   40     2      > FE_RESET_R                                       $6      !0, ->25
          3    > > FE_FETCH_R                                       ~7      $6, !2, ->25
          4    >   ASSIGN                                                   !3, ~7
   41     5        TYPE_CHECK                                  128  ~9      !2
          6      > JMPZ_EX                                          ~9      ~9, ->10
          7    >   COUNT                                            ~10     !2
          8        IS_IDENTICAL                                     ~11     ~10, 1
          9        BOOL                                             ~9      ~11
         10    > > JMPZ                                                     ~9, ->22
   42    11    >   INIT_FCALL                                               'array_key_first'
         12        SEND_VAR                                                 !2
         13        DO_ICALL                                         $12     
         14        ASSIGN                                                   !4, $12
   43    15        ROPE_INIT                                     3  ~15     !3
         16        ROPE_ADD                                      1  ~15     ~15, '_'
         17        ROPE_END                                      2  ~14     ~15, !4
         18        FETCH_DIM_R                                      ~18     !2, !4
         19        ASSIGN_DIM                                               !1, ~14
         20        OP_DATA                                                  ~18
   41    21      > JMP                                                      ->24
   45    22    >   ASSIGN_DIM                                               !1, !3
         23        OP_DATA                                                  !2
   40    24    > > JMP                                                      ->3
         25    >   FE_FREE                                                  $6
   49    26        VERIFY_RETURN_TYPE                                       !1
         27      > RETURN                                                   !1
   50    28*       VERIFY_RETURN_TYPE                                       
         29*     > RETURN                                                   null

End of function mergesingleelementarrays

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
156.92 ms | 1031 KiB | 21 Q