3v4l.org

run code in 300+ 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 = 17, Position 2 = 27
Branch analysis from position: 17
2 jumps found. (Code = 46) Position 1 = 28, Position 2 = 51
Branch analysis from position: 28
2 jumps found. (Code = 43) Position 1 = 52, Position 2 = 67
Branch analysis from position: 52
1 jumps found. (Code = 42) Position 1 = 68
Branch analysis from position: 68
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 67
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 51
Branch analysis from position: 27
filename:       /in/4KdLs
function name:  {closure}
number of ops:  72
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                                               'in_array'
          8        SEND_VAL                                                 <true>
          9        INIT_FCALL                                               'array_map'
         10        SEND_VAL                                                 'is_array'
         11        SEND_VAR                                                 !1
         12        DO_ICALL                                         $7      
         13        SEND_VAR                                                 $7
         14        SEND_VAL                                                 <true>
         15        DO_ICALL                                         $8      
         16      > JMPZ_EX                                          ~9      $8, ->27
   46    17    >   INIT_FCALL                                               'array_combine'
         18        INIT_FCALL                                               'array_keys'
         19        SEND_VAR                                                 !1
         20        DO_ICALL                                         $10     
         21        ASSIGN                                           ~11     !3, $10
         22        SEND_VAL                                                 ~11
         23        SEND_VAR                                                 !3
         24        DO_ICALL                                         $12     
         25        ASSIGN                                           ~13     !2, $12
         26        BOOL                                             ~9      ~13
         27    > > JMPZ_EX                                          ~9      ~9, ->51
   47    28    >   INIT_FCALL                                               'array_keys'
         29        INIT_FCALL                                               'array_flip'
         30        INIT_FCALL                                               'array_merge'
         31        INIT_FCALL                                               'array_values'
         32        INIT_FCALL                                               'array_map'
         33        SEND_VAL                                                 'array_keys'
         34        INIT_FCALL                                               'array_filter'
         35        SEND_VAR                                                 !1
         36        SEND_VAL                                                 'is_array'
         37        DO_ICALL                                         $14     
         38        SEND_VAR                                                 $14
         39        DO_ICALL                                         $15     
         40        SEND_VAR                                                 $15
         41        DO_ICALL                                         $16     
         42        SEND_UNPACK                                              $16
         43        CHECK_UNDEF_ARGS                                         
         44        DO_ICALL                                         $17     
         45        SEND_VAR                                                 $17
         46        DO_ICALL                                         $18     
         47        SEND_VAR                                                 $18
         48        DO_ICALL                                         $19     
         49        ASSIGN                                           ~20     !4, $19
         50        BOOL                                             ~9      ~20
         51    > > JMPZ                                                     ~9, ->67
   49    52    >   INIT_FCALL                                               'array_map'
   50    53        DECLARE_LAMBDA_FUNCTION                          ~21     [0]
         54        BIND_LEXICAL                                             ~21, !0
         55        BIND_LEXICAL                                             ~21, !1
         56        BIND_LEXICAL                                             ~21, !5
         57        BIND_LEXICAL                                             ~21, !2
         58        SEND_VAL                                                 ~21
   51    59        INIT_FCALL                                               'array_combine'
         60        SEND_VAR                                                 !4
         61        SEND_VAR                                                 !4
         62        DO_ICALL                                         $22     
         63        SEND_VAR                                                 $22
   49    64        DO_ICALL                                         $23     
   51    65        QM_ASSIGN                                        ~24     $23
         66      > JMP                                                      ->68
   53    67    >   QM_ASSIGN                                        ~24     !1
         68    >   VERIFY_RETURN_TYPE                                       ~24
         69      > RETURN                                                   ~24
         70*       VERIFY_RETURN_TYPE                                       
         71*     > 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}
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}
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.0.0


preferences:
150.18 ms | 1027 KiB | 24 Q