3v4l.org

run code in 300+ PHP versions simultaneously
<?php function initArray() { $arr = []; for ($i = 0; $i < 20000; $i++) { $arr[] = ['name' => 'John', 'email' => 'john@me.com']; } return $arr; } function flatten1($array, $depth = INF) { $result = []; foreach ($array as $item) { $item = $item instanceof Collection ? $item->all() : $item; if (! is_array($item)) { $result[] = $item; } else { $values = $depth === 1 ? array_values($item) : flatten1($item, $depth - 1); foreach ($values as $value) { $result[] = $value; } } } return $result; } function flatten2($array, $depth = INF) { $result = []; foreach ($array as $item) { $item = $item instanceof Collection ? $item->all() : $item; if (! is_array($item)) { $result[] = $item; } else { $values = $depth === 1 ? array_values($item) : flatten2($item, $depth - 1); array_push($result, ...$values); } } return $result; } echo PHP_EOL."CONCAT".PHP_EOL; $arr = initArray(); $start = microtime(true); flatten1($arr); var_export(microtime(true) - $start); echo PHP_EOL."ARRAY_PUSH".PHP_EOL; $arr = initArray(); $start = microtime(true); flatten2($arr); var_export(microtime(true) - $start);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/AH1ZX
function name:  (null)
number of ops:  37
compiled vars:  !0 = $arr, !1 = $start
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   48     0  E >   ECHO                                                     '%0ACONCAT%0A'
   49     1        INIT_FCALL                                               'initarray'
          2        DO_FCALL                                      0  $2      
          3        ASSIGN                                                   !0, $2
   50     4        INIT_FCALL                                               'microtime'
          5        SEND_VAL                                                 <true>
          6        DO_ICALL                                         $4      
          7        ASSIGN                                                   !1, $4
   51     8        INIT_FCALL                                               'flatten1'
          9        SEND_VAR                                                 !0
         10        DO_FCALL                                      0          
   52    11        INIT_FCALL                                               'var_export'
         12        INIT_FCALL                                               'microtime'
         13        SEND_VAL                                                 <true>
         14        DO_ICALL                                         $7      
         15        SUB                                              ~8      $7, !1
         16        SEND_VAL                                                 ~8
         17        DO_ICALL                                                 
   54    18        ECHO                                                     '%0AARRAY_PUSH%0A'
   55    19        INIT_FCALL                                               'initarray'
         20        DO_FCALL                                      0  $10     
         21        ASSIGN                                                   !0, $10
   56    22        INIT_FCALL                                               'microtime'
         23        SEND_VAL                                                 <true>
         24        DO_ICALL                                         $12     
         25        ASSIGN                                                   !1, $12
   57    26        INIT_FCALL                                               'flatten2'
         27        SEND_VAR                                                 !0
         28        DO_FCALL                                      0          
   58    29        INIT_FCALL                                               'var_export'
         30        INIT_FCALL                                               'microtime'
         31        SEND_VAL                                                 <true>
         32        DO_ICALL                                         $15     
         33        SUB                                              ~16     $15, !1
         34        SEND_VAL                                                 ~16
         35        DO_ICALL                                                 
         36      > RETURN                                                   1

Function initarray:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
2 jumps found. (Code = 44) Position 1 = 8, Position 2 = 3
Branch analysis from position: 8
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 3
2 jumps found. (Code = 44) Position 1 = 8, Position 2 = 3
Branch analysis from position: 8
Branch analysis from position: 3
filename:       /in/AH1ZX
function name:  initArray
number of ops:  10
compiled vars:  !0 = $arr, !1 = $i
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    5     0  E >   ASSIGN                                                   !0, <array>
    6     1        ASSIGN                                                   !1, 0
          2      > JMP                                                      ->6
    7     3    >   ASSIGN_DIM                                               !0
          4        OP_DATA                                                  <array>
    6     5        PRE_INC                                                  !1
          6    >   IS_SMALLER                                               !1, 20000
          7      > JMPNZ                                                    ~6, ->3
    9     8    > > RETURN                                                   !0
   10     9*     > RETURN                                                   null

End of function initarray

Function flatten1:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 40
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 40
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 39
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 26
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
2 jumps found. (Code = 77) Position 1 = 34, Position 2 = 38
Branch analysis from position: 34
2 jumps found. (Code = 78) Position 1 = 35, Position 2 = 38
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 34
Branch analysis from position: 34
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 38
Branch analysis from position: 26
2 jumps found. (Code = 77) Position 1 = 34, Position 2 = 38
Branch analysis from position: 34
Branch analysis from position: 38
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
Branch analysis from position: 19
Branch analysis from position: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
filename:       /in/AH1ZX
function name:  flatten1
number of ops:  43
compiled vars:  !0 = $array, !1 = $depth, !2 = $result, !3 = $item, !4 = $values, !5 = $value
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <const ast>
   14     2        ASSIGN                                                   !2, <array>
   15     3      > FE_RESET_R                                       $7      !0, ->40
          4    > > FE_FETCH_R                                               $7, !3, ->40
   16     5    >   INSTANCEOF                                               !3, 'Collection'
          6      > JMPZ                                                     ~8, ->11
          7    >   INIT_METHOD_CALL                                         !3, 'all'
          8        DO_FCALL                                      0  $9      
          9        QM_ASSIGN                                        ~10     $9
         10      > JMP                                                      ->12
         11    >   QM_ASSIGN                                        ~10     !3
         12    >   ASSIGN                                                   !3, ~10
   17    13        TYPE_CHECK                                  128  ~12     !3
         14        BOOL_NOT                                         ~13     ~12
         15      > JMPZ                                                     ~13, ->19
   18    16    >   ASSIGN_DIM                                               !2
         17        OP_DATA                                                  !3
         18      > JMP                                                      ->39
   20    19    >   IS_IDENTICAL                                             !1, 1
         20      > JMPZ                                                     ~15, ->26
   21    21    >   INIT_FCALL                                               'array_values'
         22        SEND_VAR                                                 !3
         23        DO_ICALL                                         $16     
         24        QM_ASSIGN                                        ~17     $16
         25      > JMP                                                      ->32
   22    26    >   INIT_FCALL_BY_NAME                                       'flatten1'
         27        SEND_VAR_EX                                              !3
         28        SUB                                              ~18     !1, 1
         29        SEND_VAL_EX                                              ~18
         30        DO_FCALL                                      0  $19     
         31        QM_ASSIGN                                        ~17     $19
   20    32    >   ASSIGN                                                   !4, ~17
   23    33      > FE_RESET_R                                       $21     !4, ->38
         34    > > FE_FETCH_R                                               $21, !5, ->38
   24    35    >   ASSIGN_DIM                                               !2
         36        OP_DATA                                                  !5
   23    37      > JMP                                                      ->34
         38    >   FE_FREE                                                  $21
   15    39    > > JMP                                                      ->4
         40    >   FE_FREE                                                  $7
   28    41      > RETURN                                                   !2
   29    42*     > RETURN                                                   null

End of function flatten1

Function flatten2:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 4, Position 2 = 39
Branch analysis from position: 4
2 jumps found. (Code = 78) Position 1 = 5, Position 2 = 39
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 7, Position 2 = 11
Branch analysis from position: 7
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 38
Branch analysis from position: 38
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 19
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 26
Branch analysis from position: 21
1 jumps found. (Code = 42) Position 1 = 32
Branch analysis from position: 32
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 26
1 jumps found. (Code = 42) Position 1 = 4
Branch analysis from position: 4
Branch analysis from position: 11
2 jumps found. (Code = 43) Position 1 = 16, Position 2 = 19
Branch analysis from position: 16
Branch analysis from position: 19
Branch analysis from position: 39
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 39
filename:       /in/AH1ZX
function name:  flatten2
number of ops:  42
compiled vars:  !0 = $array, !1 = $depth, !2 = $result, !3 = $item, !4 = $values
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   31     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <const ast>
   33     2        ASSIGN                                                   !2, <array>
   34     3      > FE_RESET_R                                       $6      !0, ->39
          4    > > FE_FETCH_R                                               $6, !3, ->39
   35     5    >   INSTANCEOF                                               !3, 'Collection'
          6      > JMPZ                                                     ~7, ->11
          7    >   INIT_METHOD_CALL                                         !3, 'all'
          8        DO_FCALL                                      0  $8      
          9        QM_ASSIGN                                        ~9      $8
         10      > JMP                                                      ->12
         11    >   QM_ASSIGN                                        ~9      !3
         12    >   ASSIGN                                                   !3, ~9
   36    13        TYPE_CHECK                                  128  ~11     !3
         14        BOOL_NOT                                         ~12     ~11
         15      > JMPZ                                                     ~12, ->19
   37    16    >   ASSIGN_DIM                                               !2
         17        OP_DATA                                                  !3
         18      > JMP                                                      ->38
   39    19    >   IS_IDENTICAL                                             !1, 1
         20      > JMPZ                                                     ~14, ->26
   40    21    >   INIT_FCALL                                               'array_values'
         22        SEND_VAR                                                 !3
         23        DO_ICALL                                         $15     
         24        QM_ASSIGN                                        ~16     $15
         25      > JMP                                                      ->32
   41    26    >   INIT_FCALL_BY_NAME                                       'flatten2'
         27        SEND_VAR_EX                                              !3
         28        SUB                                              ~17     !1, 1
         29        SEND_VAL_EX                                              ~17
         30        DO_FCALL                                      0  $18     
         31        QM_ASSIGN                                        ~16     $18
   39    32    >   ASSIGN                                                   !4, ~16
   42    33        INIT_FCALL                                               'array_push'
         34        SEND_REF                                                 !2
         35        SEND_UNPACK                                              !4
         36        CHECK_UNDEF_ARGS                                         
         37        DO_ICALL                                                 
   34    38    > > JMP                                                      ->4
         39    >   FE_FREE                                                  $6
   45    40      > RETURN                                                   !2
   46    41*     > RETURN                                                   null

End of function flatten2

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
165.22 ms | 1411 KiB | 25 Q