3v4l.org

run code in 300+ PHP versions simultaneously
<?php function mergeAttachments($a, $b) { // If both #attached arrays contain drupalSettings, then merge them // correctly; adding the same settings multiple times needs to behave // idempotently. if (!empty($a['drupalSettings']) && !empty($b['drupalSettings'])) { $a['drupalSettings'] = mergeDeepArray([$a['drupalSettings'], $b['drupalSettings']], TRUE); unset($b['drupalSettings']); } return mergeDeep($a, $b); } function mergeDeepArray($arrays, $preserve_integer_keys = FALSE) { $result = array(); 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_integer($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(array($result[$key], $value), $preserve_integer_keys); } // Otherwise, use the latter value, overriding any previous value. else { $result[$key] = $value; } } } return $result; } function mergeDeep() { return mergeDeepArray(func_get_args()); } for($i = 0; $i <= 100000; $i++) { $a['#attached'] = array( 'library' => array( 'core/drupal', 'core/drupalSettings', ), 'drupalSettings' => [ 'bar' => ['a', 'b', 'c'], ], 'js' => array( 'baz.js' => array(), ), ); $b['#attached'] = array( 'library' => array( 'core/jquery', ), 'js' => array( 'foo.js' => array(), 'bar.js' => array(), ), 'drupalSettings' => [ 'foo' => ['d'], ], ); mergeAttachments($a['#attached'], $b['#attached']); }
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 42) Position 1 = 13
Branch analysis from position: 13
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 2
Branch analysis from position: 15
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 2
2 jumps found. (Code = 44) Position 1 = 15, Position 2 = 2
Branch analysis from position: 15
Branch analysis from position: 2
filename:       /in/2PgFh
function name:  (null)
number of ops:  16
compiled vars:  !0 = $i, !1 = $a, !2 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   41     0  E >   ASSIGN                                                   !0, 0
          1      > JMP                                                      ->13
   42     2    >   ASSIGN_DIM                                               !1, '%23attached'
   44     3        OP_DATA                                                  <array>
   54     4        ASSIGN_DIM                                               !2, '%23attached'
   56     5        OP_DATA                                                  <array>
   66     6        INIT_FCALL                                               'mergeattachments'
          7        FETCH_DIM_R                                      ~6      !1, '%23attached'
          8        SEND_VAL                                                 ~6
          9        FETCH_DIM_R                                      ~7      !2, '%23attached'
         10        SEND_VAL                                                 ~7
         11        DO_FCALL                                      0          
   41    12        PRE_INC                                                  !0
         13    >   IS_SMALLER_OR_EQUAL                                      !0, 100000
         14      > JMPNZ                                                    ~10, ->2
   67    15    > > RETURN                                                   1

Function mergeattachments:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 5, Position 2 = 8
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 20
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 20
Branch analysis from position: 8
filename:       /in/2PgFh
function name:  mergeAttachments
number of ops:  26
compiled vars:  !0 = $a, !1 = $b
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    3     0  E >   RECV                                             !0      
          1        RECV                                             !1      
    7     2        ISSET_ISEMPTY_DIM_OBJ                         1  ~2      !0, 'drupalSettings'
          3        BOOL_NOT                                         ~3      ~2
          4      > JMPZ_EX                                          ~3      ~3, ->8
          5    >   ISSET_ISEMPTY_DIM_OBJ                         1  ~4      !1, 'drupalSettings'
          6        BOOL_NOT                                         ~5      ~4
          7        BOOL                                             ~3      ~5
          8    > > JMPZ                                                     ~3, ->20
    8     9    >   INIT_FCALL_BY_NAME                                       'mergeDeepArray'
         10        FETCH_DIM_R                                      ~7      !0, 'drupalSettings'
         11        INIT_ARRAY                                       ~8      ~7
         12        FETCH_DIM_R                                      ~9      !1, 'drupalSettings'
         13        ADD_ARRAY_ELEMENT                                ~8      ~9
         14        SEND_VAL_EX                                              ~8
         15        SEND_VAL_EX                                              <true>
         16        DO_FCALL                                      0  $10     
         17        ASSIGN_DIM                                               !0, 'drupalSettings'
         18        OP_DATA                                                  $10
    9    19        UNSET_DIM                                                !1, 'drupalSettings'
   11    20    >   INIT_FCALL_BY_NAME                                       'mergeDeep'
         21        SEND_VAR_EX                                              !0
         22        SEND_VAR_EX                                              !1
         23        DO_FCALL                                      0  $11     
         24      > RETURN                                                   $11
   12    25*     > RETURN                                                   null

End of function mergeattachments

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/2PgFh
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
-------------------------------------------------------------------------------------
   14     0  E >   RECV                                             !0      
          1        RECV_INIT                                        !1      <false>
   15     2        ASSIGN                                                   !2, <array>
   16     3      > FE_RESET_R                                       $7      !0, ->40
          4    > > FE_FETCH_R                                               $7, !3, ->40
   17     5    > > FE_RESET_R                                       $8      !3, ->38
          6    > > FE_FETCH_R                                       ~9      $8, !4, ->38
          7    >   ASSIGN                                                   !5, ~9
   21     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
   22    13    >   ASSIGN_DIM                                               !2
         14        OP_DATA                                                  !4
         15      > JMP                                                      ->37
   25    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
   26    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
   30    35    >   ASSIGN_DIM                                               !2, !5
         36        OP_DATA                                                  !4
   17    37    > > JMP                                                      ->6
         38    >   FE_FREE                                                  $8
   16    39      > JMP                                                      ->4
         40    >   FE_FREE                                                  $7
   34    41      > RETURN                                                   !2
   35    42*     > RETURN                                                   null

End of function mergedeeparray

Function mergedeep:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/2PgFh
function name:  mergeDeep
number of ops:  6
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   38     0  E >   INIT_FCALL                                               'mergedeeparray'
          1        FUNC_GET_ARGS                                    ~0      
          2        SEND_VAL                                                 ~0
          3        DO_FCALL                                      0  $1      
          4      > RETURN                                                   $1
   39     5*     > RETURN                                                   null

End of function mergedeep

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
160.08 ms | 1398 KiB | 15 Q