3v4l.org

run code in 300+ PHP versions simultaneously
<?php // Simple array merging $array1 = [10, 50, 20, 40, 30]; $array2 = [1.0, 5.0, 2.0, 4.0, 3.0]; print_r(array_merge($array1, $array2)); // Sorting table-like arrays that has sorted by key elements $table1 = [ '2011:london' => ['city' => 'London', 'year' => 2011, 'birthrate' => 'over9000'], '2012:birmingham' => ['city' => 'Birmingham', 'year' => 2012, 'birthrate' => 'wat'], '2013:london' => ['city' => 'London', 'year' => 2013, 'birthrate' => 'boo boo'] ]; $table2 = [ '2011:london' => ['city' => 'London', 'year' => 2011, 'comment' => 1], '2012:london' => ['city' => 'London', 'year' => 2012, 'comment' => 22], '2014:london' => ['city' => 'London', 'year' => 2014, 'comment' => 333] ]; print_r(array_linear_merge($table1, $table2)); print_r(array_linear_merge2($table1, $table2)); // New func for merging sorted by unique key table-like arrays! // Complexity O(m+n), where m and n are sizes of each array. function array_linear_merge($array1, $array2) { $iterator1 = new ArrayIterator($array1); $iterator2 = new ArrayIterator($array2); $result = []; while ($iterator1->valid() && $iterator2->valid()) { switch(sign(strcmp($iterator1->key(), $iterator2->key()))) { case 1: $result[] = $iterator2->current(); $iterator2->next(); break; case 0: $result[] = array_merge($iterator1->current(), $iterator2->current()); $iterator1->next(); $iterator2->next(); break; case -1: $result[] = $iterator1->current(); $iterator1->next(); break; } } if (!$iterator1->valid()) { $iterator1 = $iterator2; } while ($iterator1->valid()) { $result[] = $iterator1->current(); $iterator1->next(); } return $result; } function array_linear_merge2($array1, $array2) { reset($array1); reset($array2); $result = []; while (current($array1) !== false && current($array2) !== false) { switch(sign(strcmp(key($array1), key($array2)))) { case 1: $result[] = current($array2); next($array2); break; case 0: $result[] = array_merge(current($array1), current($array2)); next($array1); next($array2); break; case -1: $result[] = current($array1); next($array1); break; } } while ($next = next($array1)) { $result[] = $next; } while ($next = next($array2)) { $result[] = $next; } return $result; } function sign($number) { return ($number > 0) - ($number < 0); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dP0s7
function name:  (null)
number of ops:  26
compiled vars:  !0 = $array1, !1 = $array2, !2 = $table1, !3 = $table2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   ASSIGN                                                   !0, <array>
    5     1        ASSIGN                                                   !1, <array>
    6     2        INIT_FCALL                                               'print_r'
          3        INIT_FCALL                                               'array_merge'
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !1
          6        DO_ICALL                                         $6      
          7        SEND_VAR                                                 $6
          8        DO_ICALL                                                 
    9     9        ASSIGN                                                   !2, <array>
   14    10        ASSIGN                                                   !3, <array>
   19    11        INIT_FCALL                                               'print_r'
         12        INIT_FCALL_BY_NAME                                       'array_linear_merge'
         13        SEND_VAR_EX                                              !2
         14        SEND_VAR_EX                                              !3
         15        DO_FCALL                                      0  $10     
         16        SEND_VAR                                                 $10
         17        DO_ICALL                                                 
   20    18        INIT_FCALL                                               'print_r'
         19        INIT_FCALL_BY_NAME                                       'array_linear_merge2'
         20        SEND_VAR_EX                                              !2
         21        SEND_VAR_EX                                              !3
         22        DO_FCALL                                      0  $12     
         23        SEND_VAR                                                 $12
         24        DO_ICALL                                                 
   81    25      > RETURN                                                   1

Function array_linear_merge:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 60
Branch analysis from position: 60
2 jumps found. (Code = 46) Position 1 = 63, Position 2 = 66
Branch analysis from position: 63
2 jumps found. (Code = 44) Position 1 = 67, Position 2 = 12
Branch analysis from position: 67
2 jumps found. (Code = 43) Position 1 = 71, Position 2 = 72
Branch analysis from position: 71
1 jumps found. (Code = 42) Position 1 = 79
Branch analysis from position: 79
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 73
Branch analysis from position: 82
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 73
2 jumps found. (Code = 44) Position 1 = 82, Position 2 = 73
Branch analysis from position: 82
Branch analysis from position: 73
Branch analysis from position: 72
Branch analysis from position: 12
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 30
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 37
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 52
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
2 jumps found. (Code = 46) Position 1 = 63, Position 2 = 66
Branch analysis from position: 63
Branch analysis from position: 66
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 59
Branch analysis from position: 59
Branch analysis from position: 66
filename:       /in/dP0s7
function name:  array_linear_merge
number of ops:  84
compiled vars:  !0 = $array1, !1 = $array2, !2 = $iterator1, !3 = $iterator2, !4 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   24     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   25     2        NEW                                              $5      'ArrayIterator'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $5
   26     6        NEW                                              $8      'ArrayIterator'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !3, $8
   27    10        ASSIGN                                                   !4, <array>
   28    11      > JMP                                                      ->60
   29    12    >   INIT_FCALL_BY_NAME                                       'sign'
         13        INIT_FCALL                                               'strcmp'
         14        INIT_METHOD_CALL                                         !2, 'key'
         15        DO_FCALL                                      0  $12     
         16        SEND_VAR                                                 $12
         17        INIT_METHOD_CALL                                         !3, 'key'
         18        DO_FCALL                                      0  $13     
         19        SEND_VAR                                                 $13
         20        DO_ICALL                                         $14     
         21        SEND_VAR_NO_REF_EX                                       $14
         22        DO_FCALL                                      0  $15     
   30    23        CASE                                                     $15, 1
         24      > JMPNZ                                                    ~16, ->30
   33    25    >   CASE                                                     $15, 0
         26      > JMPNZ                                                    ~16, ->37
   29    27    >   CASE                                                     $15, -1
         28      > JMPNZ                                                    ~16, ->52
         29    > > JMP                                                      ->59
   30    30    >   INIT_METHOD_CALL                                         !3, 'current'
         31        DO_FCALL                                      0  $18     
         32        ASSIGN_DIM                                               !4
         33        OP_DATA                                                  $18
   31    34        INIT_METHOD_CALL                                         !3, 'next'
         35        DO_FCALL                                      0          
   32    36      > JMP                                                      ->59
   33    37    >   INIT_FCALL                                               'array_merge'
         38        INIT_METHOD_CALL                                         !2, 'current'
         39        DO_FCALL                                      0  $21     
         40        SEND_VAR                                                 $21
         41        INIT_METHOD_CALL                                         !3, 'current'
         42        DO_FCALL                                      0  $22     
         43        SEND_VAR                                                 $22
         44        DO_ICALL                                         $23     
         45        ASSIGN_DIM                                               !4
         46        OP_DATA                                                  $23
   34    47        INIT_METHOD_CALL                                         !2, 'next'
         48        DO_FCALL                                      0          
   35    49        INIT_METHOD_CALL                                         !3, 'next'
         50        DO_FCALL                                      0          
   36    51      > JMP                                                      ->59
   37    52    >   INIT_METHOD_CALL                                         !2, 'current'
         53        DO_FCALL                                      0  $27     
         54        ASSIGN_DIM                                               !4
         55        OP_DATA                                                  $27
   38    56        INIT_METHOD_CALL                                         !2, 'next'
         57        DO_FCALL                                      0          
   39    58      > JMP                                                      ->59
         59    >   FREE                                                     $15
   28    60    >   INIT_METHOD_CALL                                         !2, 'valid'
         61        DO_FCALL                                      0  $29     
         62      > JMPZ_EX                                          ~30     $29, ->66
         63    >   INIT_METHOD_CALL                                         !3, 'valid'
         64        DO_FCALL                                      0  $31     
         65        BOOL                                             ~30     $31
         66    > > JMPNZ                                                    ~30, ->12
   42    67    >   INIT_METHOD_CALL                                         !2, 'valid'
         68        DO_FCALL                                      0  $32     
         69        BOOL_NOT                                         ~33     $32
         70      > JMPZ                                                     ~33, ->72
   43    71    >   ASSIGN                                                   !2, !3
   45    72    > > JMP                                                      ->79
   46    73    >   INIT_METHOD_CALL                                         !2, 'current'
         74        DO_FCALL                                      0  $36     
         75        ASSIGN_DIM                                               !4
         76        OP_DATA                                                  $36
   47    77        INIT_METHOD_CALL                                         !2, 'next'
         78        DO_FCALL                                      0          
   45    79    >   INIT_METHOD_CALL                                         !2, 'valid'
         80        DO_FCALL                                      0  $38     
         81      > JMPNZ                                                    $38, ->73
   49    82    > > RETURN                                                   !4
   50    83*     > RETURN                                                   null

End of function array_linear_merge

Function array_linear_merge2:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 68
Branch analysis from position: 68
2 jumps found. (Code = 46) Position 1 = 73, Position 2 = 78
Branch analysis from position: 73
2 jumps found. (Code = 44) Position 1 = 79, Position 2 = 10
Branch analysis from position: 79
1 jumps found. (Code = 42) Position 1 = 82
Branch analysis from position: 82
2 jumps found. (Code = 44) Position 1 = 87, Position 2 = 80
Branch analysis from position: 87
1 jumps found. (Code = 42) Position 1 = 90
Branch analysis from position: 90
2 jumps found. (Code = 44) Position 1 = 95, Position 2 = 88
Branch analysis from position: 95
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 88
2 jumps found. (Code = 44) Position 1 = 95, Position 2 = 88
Branch analysis from position: 95
Branch analysis from position: 88
Branch analysis from position: 80
2 jumps found. (Code = 44) Position 1 = 87, Position 2 = 80
Branch analysis from position: 87
Branch analysis from position: 80
Branch analysis from position: 10
2 jumps found. (Code = 44) Position 1 = 25, Position 2 = 30
Branch analysis from position: 25
2 jumps found. (Code = 44) Position 1 = 27, Position 2 = 39
Branch analysis from position: 27
2 jumps found. (Code = 44) Position 1 = 29, Position 2 = 58
Branch analysis from position: 29
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
2 jumps found. (Code = 46) Position 1 = 73, Position 2 = 78
Branch analysis from position: 73
Branch analysis from position: 78
Branch analysis from position: 58
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
Branch analysis from position: 39
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
Branch analysis from position: 30
1 jumps found. (Code = 42) Position 1 = 67
Branch analysis from position: 67
Branch analysis from position: 78
filename:       /in/dP0s7
function name:  array_linear_merge2
number of ops:  97
compiled vars:  !0 = $array1, !1 = $array2, !2 = $result, !3 = $next
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   52     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   53     2        INIT_FCALL                                               'reset'
          3        SEND_REF                                                 !0
          4        DO_ICALL                                                 
   54     5        INIT_FCALL                                               'reset'
          6        SEND_REF                                                 !1
          7        DO_ICALL                                                 
   55     8        ASSIGN                                                   !2, <array>
   56     9      > JMP                                                      ->68
   57    10    >   INIT_FCALL_BY_NAME                                       'sign'
         11        INIT_FCALL                                               'strcmp'
         12        INIT_FCALL                                               'key'
         13        SEND_VAR                                                 !0
         14        DO_ICALL                                         $7      
         15        SEND_VAR                                                 $7
         16        INIT_FCALL                                               'key'
         17        SEND_VAR                                                 !1
         18        DO_ICALL                                         $8      
         19        SEND_VAR                                                 $8
         20        DO_ICALL                                         $9      
         21        SEND_VAR_NO_REF_EX                                       $9
         22        DO_FCALL                                      0  $10     
   58    23        CASE                                                     $10, 1
         24      > JMPNZ                                                    ~11, ->30
   61    25    >   CASE                                                     $10, 0
         26      > JMPNZ                                                    ~11, ->39
   57    27    >   CASE                                                     $10, -1
         28      > JMPNZ                                                    ~11, ->58
         29    > > JMP                                                      ->67
   58    30    >   INIT_FCALL                                               'current'
         31        SEND_VAR                                                 !1
         32        DO_ICALL                                         $13     
         33        ASSIGN_DIM                                               !2
         34        OP_DATA                                                  $13
   59    35        INIT_FCALL                                               'next'
         36        SEND_REF                                                 !1
         37        DO_ICALL                                                 
   60    38      > JMP                                                      ->67
   61    39    >   INIT_FCALL                                               'array_merge'
         40        INIT_FCALL                                               'current'
         41        SEND_VAR                                                 !0
         42        DO_ICALL                                         $16     
         43        SEND_VAR                                                 $16
         44        INIT_FCALL                                               'current'
         45        SEND_VAR                                                 !1
         46        DO_ICALL                                         $17     
         47        SEND_VAR                                                 $17
         48        DO_ICALL                                         $18     
         49        ASSIGN_DIM                                               !2
         50        OP_DATA                                                  $18
   62    51        INIT_FCALL                                               'next'
         52        SEND_REF                                                 !0
         53        DO_ICALL                                                 
   63    54        INIT_FCALL                                               'next'
         55        SEND_REF                                                 !1
         56        DO_ICALL                                                 
   64    57      > JMP                                                      ->67
   65    58    >   INIT_FCALL                                               'current'
         59        SEND_VAR                                                 !0
         60        DO_ICALL                                         $22     
         61        ASSIGN_DIM                                               !2
         62        OP_DATA                                                  $22
   66    63        INIT_FCALL                                               'next'
         64        SEND_REF                                                 !0
         65        DO_ICALL                                                 
   67    66      > JMP                                                      ->67
         67    >   FREE                                                     $10
   56    68    >   INIT_FCALL                                               'current'
         69        SEND_VAR                                                 !0
         70        DO_ICALL                                         $24     
         71        TYPE_CHECK                                  1018  ~25     $24
         72      > JMPZ_EX                                          ~25     ~25, ->78
         73    >   INIT_FCALL                                               'current'
         74        SEND_VAR                                                 !1
         75        DO_ICALL                                         $26     
         76        TYPE_CHECK                                  1018  ~27     $26
         77        BOOL                                             ~25     ~27
         78    > > JMPNZ                                                    ~25, ->10
   70    79    > > JMP                                                      ->82
   71    80    >   ASSIGN_DIM                                               !2
         81        OP_DATA                                                  !3
   70    82    >   INIT_FCALL                                               'next'
         83        SEND_REF                                                 !0
         84        DO_ICALL                                         $29     
         85        ASSIGN                                           ~30     !3, $29
         86      > JMPNZ                                                    ~30, ->80
   73    87    > > JMP                                                      ->90
   74    88    >   ASSIGN_DIM                                               !2
         89        OP_DATA                                                  !3
   73    90    >   INIT_FCALL                                               'next'
         91        SEND_REF                                                 !1
         92        DO_ICALL                                         $32     
         93        ASSIGN                                           ~33     !3, $32
         94      > JMPNZ                                                    ~33, ->88
   76    95    > > RETURN                                                   !2
   77    96*     > RETURN                                                   null

End of function array_linear_merge2

Function sign:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/dP0s7
function name:  sign
number of ops:  6
compiled vars:  !0 = $number
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   79     0  E >   RECV                                             !0      
   80     1        IS_SMALLER                                       ~1      0, !0
          2        IS_SMALLER                                       ~2      !0, 0
          3        SUB                                              ~3      ~1, ~2
          4      > RETURN                                                   ~3
   81     5*     > RETURN                                                   null

End of function sign

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
145.52 ms | 1421 KiB | 27 Q