3v4l.org

run code in 300+ PHP versions simultaneously
<?php function flatten($array, $prefix = '') { $return = []; foreach ($array as $key => $value) { if (is_array($value)) { $return = array_merge($return, flatten($value, $prefix . $key . '_')); } else { $return[$prefix . $key] = $value; } } return $return; } // proof that it's working on the simple cases var_dump(flatten([ 1, 2, 3 => [4, 5] ])); var_dump(flatten([ [1, 2], [3, 4] ])); // use array_values on the result if you don't care about keys // simple multidimensional array var_dump(flatten([ 'a' => [ 'b' => 'value', ] ])); // proof that it's not overwriting values like all the other solutions var_dump(flatten([ 'a' => [ 'b' => 'value', ], 'c' => [ 'b' => 'anothervalue', ], ]));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0B8hf
function name:  (null)
number of ops:  25
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   INIT_FCALL                                               'var_dump'
          1        INIT_FCALL                                               'flatten'
   18     2        SEND_VAL                                                 <array>
   17     3        DO_FCALL                                      0  $0      
   18     4        SEND_VAR                                                 $0
   17     5        DO_ICALL                                                 
   20     6        INIT_FCALL                                               'var_dump'
          7        INIT_FCALL                                               'flatten'
   21     8        SEND_VAL                                                 <array>
   20     9        DO_FCALL                                      0  $2      
   21    10        SEND_VAR                                                 $2
   20    11        DO_ICALL                                                 
   25    12        INIT_FCALL                                               'var_dump'
         13        INIT_FCALL                                               'flatten'
   27    14        SEND_VAL                                                 <array>
   25    15        DO_FCALL                                      0  $4      
   27    16        SEND_VAR                                                 $4
   25    17        DO_ICALL                                                 
   32    18        INIT_FCALL                                               'var_dump'
         19        INIT_FCALL                                               'flatten'
   34    20        SEND_VAL                                                 <array>
   32    21        DO_FCALL                                      0  $6      
   34    22        SEND_VAR                                                 $6
   32    23        DO_ICALL                                                 
   39    24      > RETURN                                                   1

Function flatten:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 24
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 24
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 8, Position 2 = 20
Branch analysis from position: 8
1 jumps found. (Code = 42) Position 1 = 23
Branch analysis from position: 23
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 20
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 24
filename:       /in/0B8hf
function name:  flatten
number of ops:  27
compiled vars:  !0 = $array, !1 = $prefix, !2 = $return, !3 = $value, !4 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      ''
    4     2        ASSIGN                                                   !2, <array>
    5     3      > FE_RESET_R                                       $6      !0, ->24
          4    > > FE_FETCH_R                                       ~7      $6, !3, ->24
          5    >   ASSIGN                                                   !4, ~7
    6     6        TYPE_CHECK                                  128          !3
          7      > JMPZ                                                     ~9, ->20
    7     8    >   INIT_FCALL                                               'array_merge'
          9        SEND_VAR                                                 !2
         10        INIT_FCALL_BY_NAME                                       'flatten'
         11        SEND_VAR_EX                                              !3
         12        CONCAT                                           ~10     !1, !4
         13        CONCAT                                           ~11     ~10, '_'
         14        SEND_VAL_EX                                              ~11
         15        DO_FCALL                                      0  $12     
         16        SEND_VAR                                                 $12
         17        DO_ICALL                                         $13     
         18        ASSIGN                                                   !2, $13
    6    19      > JMP                                                      ->23
    9    20    >   CONCAT                                           ~15     !1, !4
         21        ASSIGN_DIM                                               !2, ~15
         22        OP_DATA                                                  !3
    5    23    > > JMP                                                      ->4
         24    >   FE_FREE                                                  $6
   12    25      > RETURN                                                   !2
   13    26*     > RETURN                                                   null

End of function flatten

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
150.79 ms | 1468 KiB | 19 Q