3v4l.org

run code in 300+ PHP versions simultaneously
<?php function position_merge(array $arr1,array $arr2) { function addIndex(&$item,$idx) { $item["array_index"]=$idx; } function rmIndex(&$item) { unset($item["array_index"]); } array_walk($arr1,"addIndex"); array_walk($arr2,"addIndex"); $result=array_merge($arr1,$arr2); usort($result,function($a,$b){ return $a["position"]>$b["position"]?1:($a["position"]<$b["position"]?-1:$a["array_index"]-$b["array_index"]); }); array_walk($result,"rmIndex"); return $result; } $array1 = array( array('name' => 'Bob', 'position' => 1), array('name' => 'Tim', 'position' => 1), array('name' => 'Alf', 'position' => 3), array('name' => 'Jon', 'position' => 3) ); $array2 = array( array('name' => 'Tom', 'position' => 2), array('name' => 'Joe', 'position' => 2), array('name' => 'Jim', 'position' => 2), array('name' => 'Kev', 'position' => 2) ); print_r(position_merge($array1,$array2));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTSiL
function name:  (null)
number of ops:  10
compiled vars:  !0 = $array1, !1 = $array2
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   21     0  E >   ASSIGN                                                   !0, <array>
   28     1        ASSIGN                                                   !1, <array>
   34     2        INIT_FCALL                                               'print_r'
          3        INIT_FCALL                                               'position_merge'
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
          9      > RETURN                                                   1

Function position_merge:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTSiL
function name:  position_merge
number of ops:  28
compiled vars:  !0 = $arr1, !1 = $arr2, !2 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    4     2        DECLARE_FUNCTION                                         'addindex'
    8     3        DECLARE_FUNCTION                                         'rmindex'
   12     4        INIT_FCALL                                               'array_walk'
          5        SEND_REF                                                 !0
          6        SEND_VAL                                                 'addIndex'
          7        DO_ICALL                                                 
   13     8        INIT_FCALL                                               'array_walk'
          9        SEND_REF                                                 !1
         10        SEND_VAL                                                 'addIndex'
         11        DO_ICALL                                                 
   14    12        INIT_FCALL                                               'array_merge'
         13        SEND_VAR                                                 !0
         14        SEND_VAR                                                 !1
         15        DO_ICALL                                         $5      
         16        ASSIGN                                                   !2, $5
   15    17        INIT_FCALL                                               'usort'
         18        SEND_REF                                                 !2
         19        DECLARE_LAMBDA_FUNCTION                                  '%00%7Bclosure%7D%2Fin%2FqTSiL%3A15%242'
   17    20        SEND_VAL                                                 ~7
         21        DO_ICALL                                                 
   18    22        INIT_FCALL                                               'array_walk'
         23        SEND_REF                                                 !2
         24        SEND_VAL                                                 'rmIndex'
         25        DO_ICALL                                                 
   19    26      > RETURN                                                   !2
   20    27*     > RETURN                                                   null

End of function position_merge

Function %00addindex%2Fin%2FqTSiL%3A4%240:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTSiL
function name:  addIndex
number of ops:  5
compiled vars:  !0 = $item, !1 = $idx
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    4     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    6     2        ASSIGN_DIM                                               !0, 'array_index'
          3        OP_DATA                                                  !1
    7     4      > RETURN                                                   null

End of function %00addindex%2Fin%2FqTSiL%3A4%240

Function %00rmindex%2Fin%2FqTSiL%3A8%241:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTSiL
function name:  rmIndex
number of ops:  3
compiled vars:  !0 = $item
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    8     0  E >   RECV                                             !0      
   10     1        UNSET_DIM                                                !0, 'array_index'
   11     2      > RETURN                                                   null

End of function %00rmindex%2Fin%2FqTSiL%3A8%241

Function %00%7Bclosure%7D%2Fin%2FqTSiL%3A15%242:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 8
Branch analysis from position: 6
1 jumps found. (Code = 42) Position 1 = 19
Branch analysis from position: 19
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 8
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 14
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 18
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 14
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qTSiL
function name:  {closure}
number of ops:  21
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   16     2        FETCH_DIM_R                                      ~2      !0, 'position'
          3        FETCH_DIM_R                                      ~3      !1, 'position'
          4        IS_SMALLER                                               ~3, ~2
          5      > JMPZ                                                     ~4, ->8
          6    >   QM_ASSIGN                                        ~5      1
          7      > JMP                                                      ->19
          8    >   FETCH_DIM_R                                      ~6      !0, 'position'
          9        FETCH_DIM_R                                      ~7      !1, 'position'
         10        IS_SMALLER                                               ~6, ~7
         11      > JMPZ                                                     ~8, ->14
         12    >   QM_ASSIGN                                        ~9      -1
         13      > JMP                                                      ->18
         14    >   FETCH_DIM_R                                      ~10     !0, 'array_index'
         15        FETCH_DIM_R                                      ~11     !1, 'array_index'
         16        SUB                                              ~12     ~10, ~11
         17        QM_ASSIGN                                        ~9      ~12
         18    >   QM_ASSIGN                                        ~5      ~9
         19    > > RETURN                                                   ~5
   17    20*     > RETURN                                                   null

End of function %00%7Bclosure%7D%2Fin%2FqTSiL%3A15%242

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
153.83 ms | 1407 KiB | 22 Q