3v4l.org

run code in 300+ PHP versions simultaneously
<?php $testArray = [ ['lev 1' => 'lev 1'], ['lev 2', 'lev 2 - 1', ['lev 2 - 2'], ['Key preserved' => 'lev 2 - 3']], [['lev 3 - 1', 'lev 3 - 2', 'lev 3 - 3']], [[[888 => 'lev 4']]] ]; // $element must initially be an array. Anything that is not an array will just return the accumulator. function flatten($element, $accum = []) { // If we don't have an array return the accumulator. if (!is_array($element)) { return $accum; } foreach($element as $key => $e) { // Check if the elements are an array or not. If not, append the accumulator with a key and value. if (!is_array($e)) { // Check that the key does not already exist, if it does we have to simply append to preserve values. if (array_key_exists($key, $accum)) { $accum[] = $e; } else { $accum[$key] = $e; } } // Only recurse when we have an array. if (is_array($e)) { // We pass in the accumulator and any values further down then hierachy. $vals = flatten($e, $accum); // We might get an empty array back if there was an array of arrays. if (!empty($vals)) { // We don't append our values here, we override the accumulator. The appending happens above when $e is not an // array. $accum = $vals; } } } // Return values of this array and all child arrays. return $accum; } var_dump(flatten($testArray));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/mutdR
function name:  (null)
number of ops:  8
compiled vars:  !0 = $testArray
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   47     1        INIT_FCALL                                               'var_dump'
          2        INIT_FCALL                                               'flatten'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        SEND_VAR                                                 $2
          6        DO_ICALL                                                 
          7      > RETURN                                                   1

Function flatten:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 7, Position 2 = 31
Branch analysis from position: 7
2 jumps found. (Code = 78) Position 1 = 8, Position 2 = 31
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 19
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 14, Position 2 = 17
Branch analysis from position: 14
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 30
Branch analysis from position: 21
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 30
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 7
Branch analysis from position: 7
Branch analysis from position: 30
Branch analysis from position: 30
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 30
Branch analysis from position: 21
Branch analysis from position: 30
Branch analysis from position: 19
Branch analysis from position: 31
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 31
filename:       /in/mutdR
function name:  flatten
number of ops:  34
compiled vars:  !0 = $element, !1 = $accum, !2 = $e, !3 = $key, !4 = $vals
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   11     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <array>
   14     2        TYPE_CHECK                                  128  ~5      !0
          3        BOOL_NOT                                         ~6      ~5
          4      > JMPZ                                                     ~6, ->6
   15     5    > > RETURN                                                   !1
   18     6    > > FE_RESET_R                                       $7      !0, ->31
          7    > > FE_FETCH_R                                       ~8      $7, !2, ->31
          8    >   ASSIGN                                                   !3, ~8
   20     9        TYPE_CHECK                                  128  ~10     !2
         10        BOOL_NOT                                         ~11     ~10
         11      > JMPZ                                                     ~11, ->19
   22    12    >   ARRAY_KEY_EXISTS                                         !3, !1
         13      > JMPZ                                                     ~12, ->17
   23    14    >   ASSIGN_DIM                                               !1
         15        OP_DATA                                                  !2
   22    16      > JMP                                                      ->19
   25    17    >   ASSIGN_DIM                                               !1, !3
         18        OP_DATA                                                  !2
   30    19    >   TYPE_CHECK                                  128          !2
         20      > JMPZ                                                     ~15, ->30
   32    21    >   INIT_FCALL_BY_NAME                                       'flatten'
         22        SEND_VAR_EX                                              !2
         23        SEND_VAR_EX                                              !1
         24        DO_FCALL                                      0  $16     
         25        ASSIGN                                                   !4, $16
   35    26        ISSET_ISEMPTY_CV                                 ~18     !4
         27        BOOL_NOT                                         ~19     ~18
         28      > JMPZ                                                     ~19, ->30
   38    29    >   ASSIGN                                                   !1, !4
   18    30    > > JMP                                                      ->7
         31    >   FE_FREE                                                  $7
   44    32      > RETURN                                                   !1
   45    33*     > RETURN                                                   null

End of function flatten

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.74 ms | 1458 KiB | 15 Q