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)); // 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 sign($number) { return ($n > 0) - ($n < 0); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/cLYt4
function name:  (null)
number of ops:  19
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                                                 
   53    18      > 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/cLYt4
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
-------------------------------------------------------------------------------------
   23     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   24     2        NEW                                              $5      'ArrayIterator'
          3        SEND_VAR_EX                                              !0
          4        DO_FCALL                                      0          
          5        ASSIGN                                                   !2, $5
   25     6        NEW                                              $8      'ArrayIterator'
          7        SEND_VAR_EX                                              !1
          8        DO_FCALL                                      0          
          9        ASSIGN                                                   !3, $8
   26    10        ASSIGN                                                   !4, <array>
   27    11      > JMP                                                      ->60
   28    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     
   29    23        CASE                                                     $15, 1
         24      > JMPNZ                                                    ~16, ->30
   32    25    >   CASE                                                     $15, 0
         26      > JMPNZ                                                    ~16, ->37
   28    27    >   CASE                                                     $15, -1
         28      > JMPNZ                                                    ~16, ->52
         29    > > JMP                                                      ->59
   29    30    >   INIT_METHOD_CALL                                         !3, 'current'
         31        DO_FCALL                                      0  $18     
         32        ASSIGN_DIM                                               !4
         33        OP_DATA                                                  $18
   30    34        INIT_METHOD_CALL                                         !3, 'next'
         35        DO_FCALL                                      0          
   31    36      > JMP                                                      ->59
   32    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
   33    47        INIT_METHOD_CALL                                         !2, 'next'
         48        DO_FCALL                                      0          
   34    49        INIT_METHOD_CALL                                         !3, 'next'
         50        DO_FCALL                                      0          
   35    51      > JMP                                                      ->59
   36    52    >   INIT_METHOD_CALL                                         !2, 'current'
         53        DO_FCALL                                      0  $27     
         54        ASSIGN_DIM                                               !4
         55        OP_DATA                                                  $27
   37    56        INIT_METHOD_CALL                                         !2, 'next'
         57        DO_FCALL                                      0          
   38    58      > JMP                                                      ->59
         59    >   FREE                                                     $15
   27    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
   41    67    >   INIT_METHOD_CALL                                         !2, 'valid'
         68        DO_FCALL                                      0  $32     
         69        BOOL_NOT                                         ~33     $32
         70      > JMPZ                                                     ~33, ->72
   42    71    >   ASSIGN                                                   !2, !3
   44    72    > > JMP                                                      ->79
   45    73    >   INIT_METHOD_CALL                                         !2, 'current'
         74        DO_FCALL                                      0  $36     
         75        ASSIGN_DIM                                               !4
         76        OP_DATA                                                  $36
   46    77        INIT_METHOD_CALL                                         !2, 'next'
         78        DO_FCALL                                      0          
   44    79    >   INIT_METHOD_CALL                                         !2, 'valid'
         80        DO_FCALL                                      0  $38     
         81      > JMPNZ                                                    $38, ->73
   48    82    > > RETURN                                                   !4
   49    83*     > RETURN                                                   null

End of function array_linear_merge

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

End of function sign

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
167.66 ms | 949 KiB | 20 Q