3v4l.org

run code in 500+ PHP versions simultaneously
<?php declare(strict_types=1); /** * https://stackoverflow.com/a/68884808/367456 */ $arr1 = [ "databases" => [ 0 => [ "id" => 1, "name" => "DB1", "slug" => "db1", "url" => "https://www.db1.org", ], ], ]; $arr2 = [ "databases" => [ 0 => [ "id" => 5, "name" => "DB2", "slug" => "db2", "url" => "https://www.db2.com", ], ], ]; /** * recursively go over two (optional) arrays old and new * while merging them. * * array values are then taken as: * * ['old' => $old, 'new' => $new]. * * @param array|null $old * @param array|null $new * @return array */ function old_and_new(array $old = null, array $new = null): array { $pair = get_defined_vars(); $map = static fn(callable $map, array $arrays): array => in_array(true, array_map('is_array', $arrays), true) && ($parameter = array_combine($k = array_keys($arrays), $k)) && ($keys = array_keys(array_flip(array_merge(...array_values(array_map('array_keys', array_filter($arrays, 'is_array')))))) ) ? array_map( static fn($key) => $map($map, array_map(static fn($p) => $arrays[$p][$key] ?? null, $parameter)), array_combine($keys, $keys) ) : $arrays; return $map($map, $pair); } print_r(old_and_new(new: $arr2));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4KdLs
function name:  (null)
number of ops:  10
compiled vars:  !0 = $arr1, !1 = $arr2
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
    7     0  E >   ASSIGN                                                       !0, <array>
   18     1        ASSIGN                                                       !1, <array>
   58     2        INIT_FCALL                                                   'print_r'
          3        INIT_FCALL                                                   'old_and_new'
          4        SEND_VAR                                                     !1, 'new'
          5        CHECK_UNDEF_ARGS                                             
          6        DO_FCALL                                          0  $4      
          7        SEND_VAR                                                     $4
          8        DO_ICALL                                                     
          9      > RETURN                                                       1

Function old_and_new:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4KdLs
function name:  old_and_new
number of ops:  20
compiled vars:  !0 = $old, !1 = $new, !2 = $pair, !3 = $map, !4 = $parameter, !5 = $k, !6 = $keys, !7 = $p, !8 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   41     0  E >   RECV_INIT                                            !0      null
          1        RECV_INIT                                            !1      null
   43     2        INIT_FCALL                                                   'get_defined_vars'
          3        DO_ICALL                                             $9      
          4        ASSIGN                                                       !2, $9
   45     5        DECLARE_LAMBDA_FUNCTION                              ~11     [0]
          6        BIND_LEXICAL                                                 ~11, !4
          7        BIND_LEXICAL                                                 ~11, !5
          8        BIND_LEXICAL                                                 ~11, !6
          9        BIND_LEXICAL                                                 ~11, !7
         10        BIND_LEXICAL                                                 ~11, !8
   44    11        ASSIGN                                                       !3, ~11
   55    12        INIT_DYNAMIC_CALL                                            !3
         13        SEND_VAR_EX                                                  !3
         14        SEND_VAR_EX                                                  !2
         15        DO_FCALL                                          0  $13     
         16        VERIFY_RETURN_TYPE                                           $13
         17      > RETURN                                                       $13
   56    18*       VERIFY_RETURN_TYPE                                           
         19*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 46) Position 1 = 14, Position 2 = 24
Branch analysis from position: 14
2 jumps found. (Code = 46) Position 1 = 25, Position 2 = 48
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 49, Position 2 = 64
Branch analysis from position: 49
1 jumps found. (Code = 42) Position 1 = 65
Branch analysis from position: 65
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 64
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 48
Branch analysis from position: 24
filename:       /in/4KdLs
function name:  {closure:old_and_new():45}
number of ops:  69
compiled vars:  !0 = $map, !1 = $arrays, !2 = $parameter, !3 = $k, !4 = $keys, !5 = $p, !6 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   45     0  E >   RECV                                                 !0      
          1        RECV                                                 !1      
          2        BIND_STATIC                                                  !2
          3        BIND_STATIC                                                  !3
          4        BIND_STATIC                                                  !4
          5        BIND_STATIC                                                  !5
          6        BIND_STATIC                                                  !6
          7        INIT_FCALL                                                   'array_map'
          8        SEND_VAL                                                     'is_array'
          9        SEND_VAR                                                     !1
         10        DO_ICALL                                             $7      
         11        FRAMELESS_ICALL_3                in_array            ~8      <true>, $7
         12        OP_DATA                                                      <true>
         13      > JMPZ_EX                                              ~8      ~8, ->24
   46    14    >   INIT_FCALL                                                   'array_combine'
         15        INIT_FCALL                                                   'array_keys'
         16        SEND_VAR                                                     !1
         17        DO_ICALL                                             $9      
         18        ASSIGN                                               ~10     !3, $9
         19        SEND_VAL                                                     ~10
         20        SEND_VAR                                                     !3
         21        DO_ICALL                                             $11     
         22        ASSIGN                                               ~12     !2, $11
         23        BOOL                                                 ~8      ~12
         24    > > JMPZ_EX                                              ~8      ~8, ->48
   47    25    >   INIT_FCALL                                                   'array_keys'
         26        INIT_FCALL                                                   'array_flip'
         27        INIT_FCALL                                                   'array_merge'
         28        INIT_FCALL                                                   'array_values'
         29        INIT_FCALL                                                   'array_map'
         30        SEND_VAL                                                     'array_keys'
         31        INIT_FCALL                                                   'array_filter'
         32        SEND_VAR                                                     !1
         33        SEND_VAL                                                     'is_array'
         34        DO_ICALL                                             $13     
         35        SEND_VAR                                                     $13
         36        DO_ICALL                                             $14     
         37        SEND_VAR                                                     $14
         38        DO_ICALL                                             $15     
         39        SEND_UNPACK                                                  $15
         40        CHECK_UNDEF_ARGS                                             
         41        DO_ICALL                                             $16     
         42        SEND_VAR                                                     $16
         43        DO_ICALL                                             $17     
         44        SEND_VAR                                                     $17
         45        DO_ICALL                                             $18     
         46        ASSIGN                                               ~19     !4, $18
         47        BOOL                                                 ~8      ~19
         48    > > JMPZ                                                         ~8, ->64
   49    49    >   INIT_FCALL                                                   'array_map'
   50    50        DECLARE_LAMBDA_FUNCTION                              ~20     [0]
         51        BIND_LEXICAL                                                 ~20, !0
         52        BIND_LEXICAL                                                 ~20, !1
         53        BIND_LEXICAL                                                 ~20, !5
         54        BIND_LEXICAL                                                 ~20, !2
         55        SEND_VAL                                                     ~20
   51    56        INIT_FCALL                                                   'array_combine'
         57        SEND_VAR                                                     !4
         58        SEND_VAR                                                     !4
         59        DO_ICALL                                             $21     
         60        SEND_VAR                                                     $21
   49    61        DO_ICALL                                             $22     
   51    62        QM_ASSIGN                                            ~23     $22
         63      > JMP                                                          ->65
   53    64    >   QM_ASSIGN                                            ~23     !1
         65    >   VERIFY_RETURN_TYPE                                           ~23
         66      > RETURN                                                       ~23
         67*       VERIFY_RETURN_TYPE                                           
         68*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4KdLs
function name:  {closure:{closure:old_and_new():45}:50}
number of ops:  18
compiled vars:  !0 = $key, !1 = $map, !2 = $arrays, !3 = $p, !4 = $parameter
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
   50     0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        BIND_STATIC                                                  !2
          3        BIND_STATIC                                                  !3
          4        BIND_STATIC                                                  !4
          5        INIT_DYNAMIC_CALL                                            !1
          6        SEND_VAR_EX                                                  !1
          7        INIT_FCALL                                                   'array_map'
          8        DECLARE_LAMBDA_FUNCTION                              ~5      [0]
          9        BIND_LEXICAL                                                 ~5, !2
         10        BIND_LEXICAL                                                 ~5, !0
         11        SEND_VAL                                                     ~5
         12        SEND_VAR                                                     !4
         13        DO_ICALL                                             $6      
         14        SEND_VAR_NO_REF_EX                                           $6
         15        DO_FCALL                                          0  $7      
         16      > RETURN                                                       $7
         17*     > RETURN                                                       null


Dynamic Functions:
Dynamic Function 0
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/4KdLs
function name:  {closure:{closure:{closure:old_and_new():45}:50}:50}
number of ops:  9
compiled vars:  !0 = $p, !1 = $arrays, !2 = $key
line      #* E I O op                               fetch          ext  return  operands
-----------------------------------------------------------------------------------------
          0  E >   RECV                                                 !0      
          1        BIND_STATIC                                                  !1
          2        BIND_STATIC                                                  !2
          3        FETCH_DIM_IS                                         ~3      !1, !0
          4        FETCH_DIM_IS                                         ~4      ~3, !2
          5        COALESCE                                             ~5      ~4
          6        QM_ASSIGN                                            ~5      null
          7      > RETURN                                                       ~5
          8*     > RETURN                                                       null

End of Dynamic Function 0

End of Dynamic Function 0

End of Dynamic Function 0

End of function old_and_new

Generated using Vulcan Logic Dumper, using php 8.5.0


preferences:
169.92 ms | 2160 KiB | 23 Q