3v4l.org

run code in 300+ PHP versions simultaneously
<?php function mergeDeepArray(array $arrays, $preserve_integer_keys = FALSE) { $result = []; foreach ($arrays as $array) { foreach ($array as $key => $value) { // Renumber integer keys as array_merge_recursive() does unless // $preserve_integer_keys is set to TRUE. Note that PHP automatically // converts array keys that are integer strings (e.g., '1') to integers. if (is_int($key) && !$preserve_integer_keys) { $result[] = $value; } // Recurse when both values are arrays. elseif (isset($result[$key]) && is_array($result[$key]) && is_array($value)) { $result[$key] = mergeDeepArray([$result[$key], $value], $preserve_integer_keys); } // Otherwise, use the latter value, overriding any previous value. else { $result[$key] = $value; } } } return $result; } $a = [['foo' => [2,3]], ['foo' => [4, 5]]]; print_r(mergeDeepArray($a)['foo']); $a = [['foo' => new ArrayObject([2,3])], ['foo' => new ArrayObject([4, 5])]]; print_r((array)mergeDeepArray($a)['foo']);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/X5Cje
function name:  (null)
number of ops:  28
compiled vars:  !0 = $a
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   26     0  E >   ASSIGN                                                   !0, <array>
   27     1        INIT_FCALL                                               'print_r'
          2        INIT_FCALL                                               'mergedeeparray'
          3        SEND_VAR                                                 !0
          4        DO_FCALL                                      0  $2      
          5        FETCH_DIM_R                                      ~3      $2, 'foo'
          6        SEND_VAL                                                 ~3
          7        DO_ICALL                                                 
   28     8        NEW                                              $5      'ArrayObject'
          9        SEND_VAL_EX                                              <array>
         10        DO_FCALL                                      0          
         11        INIT_ARRAY                                       ~7      $5, 'foo'
         12        INIT_ARRAY                                       ~8      ~7
         13        NEW                                              $9      'ArrayObject'
         14        SEND_VAL_EX                                              <array>
         15        DO_FCALL                                      0          
         16        INIT_ARRAY                                       ~11     $9, 'foo'
         17        ADD_ARRAY_ELEMENT                                ~8      ~11
         18        ASSIGN                                                   !0, ~8
   29    19        INIT_FCALL                                               'print_r'
         20        INIT_FCALL                                               'mergedeeparray'
         21        SEND_VAR                                                 !0
         22        DO_FCALL                                      0  $13     
         23        FETCH_DIM_R                                      ~14     $13, 'foo'
         24        CAST                                          7  ~15     ~14
         25        SEND_VAL                                                 ~15
         26        DO_ICALL                                                 
         27      > RETURN                                                   1

Function mergedeeparray:
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 = 77) Position 1 = 6, Position 2 = 38
Branch analysis from position: 6
2 jumps found. (Code = 78) Position 1 = 7, Position 2 = 38
Branch analysis from position: 7
2 jumps found. (Code = 46) Position 1 = 10, Position 2 = 12
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 16
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 16
2 jumps found. (Code = 46) Position 1 = 18, Position 2 = 21
Branch analysis from position: 18
2 jumps found. (Code = 46) Position 1 = 22, Position 2 = 24
Branch analysis from position: 22
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 35
Branch analysis from position: 25
1 jumps found. (Code = 42) Position 1 = 37
Branch analysis from position: 37
Branch analysis from position: 35
1 jumps found. (Code = 42) Position 1 = 6
Branch analysis from position: 6
Branch analysis from position: 24
Branch analysis from position: 21
Branch analysis from position: 12
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: 40
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 40
filename:       /in/X5Cje
function name:  mergeDeepArray
number of ops:  43
compiled vars:  !0 = $arrays, !1 = $preserve_integer_keys, !2 = $result, !3 = $array, !4 = $value, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
    4     2        ASSIGN                                                   !2, <array>
    5     3      > FE_RESET_R                                       $7      !0, ->40
          4    > > FE_FETCH_R                                               $7, !3, ->40
    6     5    > > FE_RESET_R                                       $8      !3, ->38
          6    > > FE_FETCH_R                                       ~9      $8, !4, ->38
          7    >   ASSIGN                                                   !5, ~9
   10     8        TYPE_CHECK                                   16  ~11     !5
          9      > JMPZ_EX                                          ~11     ~11, ->12
         10    >   BOOL_NOT                                         ~12     !1
         11        BOOL                                             ~11     ~12
         12    > > JMPZ                                                     ~11, ->16
   11    13    >   ASSIGN_DIM                                               !2
         14        OP_DATA                                                  !4
         15      > JMP                                                      ->37
   14    16    >   ISSET_ISEMPTY_DIM_OBJ                         0  ~14     !2, !5
         17      > JMPZ_EX                                          ~14     ~14, ->21
         18    >   FETCH_DIM_R                                      ~15     !2, !5
         19        TYPE_CHECK                                  128  ~16     ~15
         20        BOOL                                             ~14     ~16
         21    > > JMPZ_EX                                          ~14     ~14, ->24
         22    >   TYPE_CHECK                                  128  ~17     !4
         23        BOOL                                             ~14     ~17
         24    > > JMPZ                                                     ~14, ->35
   15    25    >   INIT_FCALL_BY_NAME                                       'mergeDeepArray'
         26        FETCH_DIM_R                                      ~19     !2, !5
         27        INIT_ARRAY                                       ~20     ~19
         28        ADD_ARRAY_ELEMENT                                ~20     !4
         29        SEND_VAL_EX                                              ~20
         30        SEND_VAR_EX                                              !1
         31        DO_FCALL                                      0  $21     
         32        ASSIGN_DIM                                               !2, !5
         33        OP_DATA                                                  $21
         34      > JMP                                                      ->37
   19    35    >   ASSIGN_DIM                                               !2, !5
         36        OP_DATA                                                  !4
    6    37    > > JMP                                                      ->6
         38    >   FE_FREE                                                  $8
    5    39      > JMP                                                      ->4
         40    >   FE_FREE                                                  $7
   23    41      > RETURN                                                   !2
   24    42*     > RETURN                                                   null

End of function mergedeeparray

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
130.92 ms | 1411 KiB | 17 Q