3v4l.org

run code in 500+ PHP versions simultaneously
<?php function flatten_array($array, $preserve_keys = 0, &$out = array()) { # Flatten a multidimensional array to one dimension, optionally preserving keys. # # $array - the array to flatten # $preserve_keys - 0 (default) to not preserve keys, 1 to preserve string keys only, 2 to preserve all keys # $out - internal use argument for recursion foreach($array as $key => $child) if(is_array($child)) $out = flatten_array($child, $preserve_keys, $out); elseif($preserve_keys + is_string($key) > 1) $out[$key] = $child; else $out[] = $child; return $out; } $array = array( 'first' => 1, 'second' => 2, 'query' => array( 'id' => 314, 'descripcion' => 'Test', 'files' => array( array( 'name' => 'Readme.txt', 'path' => '/tmp/43143431431343', ), array( 'name' => 'Picture.jpg', 'path' => '/tmp/98794894594655', ), ), ), 'last' => 99, ); echo "Original:\n"; var_export($array); echo "\n\nFlat:\n"; var_export( flatten_array($array, 1) );
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/aRj32
function name:  (null)
number of ops:  14
compiled vars:  !0 = $array
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   21     0  E >   ASSIGN                                                       !0, <array>
   41     1        ECHO                                                         'Original%3A%0A'
   42     2        INIT_FCALL                                                   'var_export'
          3        SEND_VAR                                                     !0
          4        DO_ICALL                                                     
   44     5        ECHO                                                         '%0A%0AFlat%3A%0A'
   45     6        INIT_FCALL                                                   'var_export'
          7        INIT_FCALL                                                   'flatten_array'
          8        SEND_VAR                                                     !0
          9        SEND_VAL                                                     1
         10        DO_FCALL                                          0  $3      
         11        SEND_VAR                                                     $3
         12        DO_ICALL                                                     
         13      > RETURN                                                       1

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

End of function flatten_array

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
164.46 ms | 3175 KiB | 15 Q