3v4l.org

run code in 500+ PHP versions simultaneously
<?php $first = [ 1 => 4.00, 2 => 3.00, 3 => 8.00, 4 => 4.88, 5 => 7.88, 10 => 17.88 ]; $second = [ 1 => 2.00, 3 => 4.00, 4 => 2.88, 7 => 5.0, 8 => 6.00 ]; // Merge the 2 original arrays and preserve the keys // https://stackoverflow.com/q/17462354/296555 // The duplicate items' value will be clobbered at this point but we don't care. We'll set them in the `array_walk` function. $output = $first + $second; // Create an array of the duplicates. We'll use these keys to calculate the difference. $both = array_intersect_key($first, $second); // Foreach element in the duplicates, calculate the difference. // Notice that we're passing in `&$output` by reference so that we are modifying the underlying object and not just a copy of it. array_walk($both, function($value, $key) use (&$output, $first, $second) { $output[$key] = $first[$key] - $second[$key]; }); // Finally, sort the final array by its keys. ksort($output); var_dump($output); // Output //array (size=8) // 1 => float 2 // 2 => float 3 // 3 => float 4 // 4 => float 2 // 5 => float 7.88 // 7 => float 5 // 8 => float 6 // 10 => float 17.88
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WD0t4
function name:  (null)
number of ops:  24
compiled vars:  !0 = $first, !1 = $second, !2 = $output, !3 = $both
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   ASSIGN                                                   !0, <array>
   12     1        ASSIGN                                                   !1, <array>
   23     2        ADD                                              ~6      !0, !1
          3        ASSIGN                                                   !2, ~6
   26     4        INIT_FCALL                                               'array_intersect_key'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $8      
          8        ASSIGN                                                   !3, $8
   30     9        INIT_FCALL                                               'array_walk'
         10        SEND_REF                                                 !3
         11        DECLARE_LAMBDA_FUNCTION                          ~10     [0]
         12        BIND_LEXICAL                                             ~10, !2
         13        BIND_LEXICAL                                             ~10, !0
         14        BIND_LEXICAL                                             ~10, !1
   32    15        SEND_VAL                                                 ~10
   30    16        DO_ICALL                                                 
   35    17        INIT_FCALL                                               'ksort'
         18        SEND_REF                                                 !2
         19        DO_ICALL                                                 
   37    20        INIT_FCALL                                               'var_dump'
         21        SEND_VAR                                                 !2
         22        DO_ICALL                                                 
   48    23      > RETURN                                                   1


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/WD0t4
function name:  {closure}
number of ops:  11
compiled vars:  !0 = $value, !1 = $key, !2 = $output, !3 = $first, !4 = $second
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   30     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        BIND_STATIC                                              !2
          3        BIND_STATIC                                              !3
          4        BIND_STATIC                                              !4
   31     5        FETCH_DIM_R                                      ~6      !3, !1
          6        FETCH_DIM_R                                      ~7      !4, !1
          7        SUB                                              ~8      ~6, ~7
          8        ASSIGN_DIM                                               !2, !1
          9        OP_DATA                                                  ~8
   32    10      > RETURN                                                   null

End of Dynamic Function 0

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
173.15 ms | 2278 KiB | 15 Q