3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * Given an array and a set of `old => new` keys, * will recursively replace all array keys that * are old with their corresponding new value. * * @param mixed $array * @param array $old_to_new_keys * * @return array */ function array_replace_keys($array, array $old_to_new_keys) { if(!is_array($array)){ return $array; } $temp_array = []; $ak = array_keys($old_to_new_keys); $av = array_values($old_to_new_keys); foreach($array as $key => $value){ if(array_search($key, $ak, true) !== false){ $key = $av[array_search($key, $ak)]; } if(is_array($value)){ $value = array_replace_keys($value, $old_to_new_keys); } $temp_array[$key] = $value; } return $temp_array; } $old = array( 'a' => 'blah', 'b' => 'key', 'c' => 'amazing', 'd' => array( 0 => 'want to replace', 1 => 'yes I want to' ) ); $replace = ["a" => "AA", 1 => 11]; var_export(array_replace_keys($old, $replace));
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/qmfFp
function name:  (null)
number of ops:  10
compiled vars:  !0 = $old, !1 = $replace
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   ASSIGN                                                   !0, <array>
   46     1        ASSIGN                                                   !1, <array>
   48     2        INIT_FCALL                                               'var_export'
          3        INIT_FCALL                                               'array_replace_keys'
          4        SEND_VAR                                                 !0
          5        SEND_VAR                                                 !1
          6        DO_FCALL                                      0  $4      
          7        SEND_VAR                                                 $4
          8        DO_ICALL                                                 
          9      > RETURN                                                   1

Function array_replace_keys:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 6
2 jumps found. (Code = 77) Position 1 = 16, Position 2 = 41
Branch analysis from position: 16
2 jumps found. (Code = 78) Position 1 = 17, Position 2 = 41
Branch analysis from position: 17
2 jumps found. (Code = 43) Position 1 = 25, Position 2 = 31
Branch analysis from position: 25
2 jumps found. (Code = 43) Position 1 = 33, Position 2 = 38
Branch analysis from position: 33
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
Branch analysis from position: 38
Branch analysis from position: 31
Branch analysis from position: 41
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 41
filename:       /in/qmfFp
function name:  array_replace_keys
number of ops:  44
compiled vars:  !0 = $array, !1 = $old_to_new_keys, !2 = $temp_array, !3 = $ak, !4 = $av, !5 = $value, !6 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   12     0  E >   RECV                                             !0      
          1        RECV                                             !1      
   14     2        TYPE_CHECK                                  128  ~7      !0
          3        BOOL_NOT                                         ~8      ~7
          4      > JMPZ                                                     ~8, ->6
   15     5    > > RETURN                                                   !0
   18     6    >   ASSIGN                                                   !2, <array>
   19     7        INIT_FCALL                                               'array_keys'
          8        SEND_VAR                                                 !1
          9        DO_ICALL                                         $10     
         10        ASSIGN                                                   !3, $10
   20    11        INIT_FCALL                                               'array_values'
         12        SEND_VAR                                                 !1
         13        DO_ICALL                                         $12     
         14        ASSIGN                                                   !4, $12
   22    15      > FE_RESET_R                                       $14     !0, ->41
         16    > > FE_FETCH_R                                       ~15     $14, !5, ->41
         17    >   ASSIGN                                                   !6, ~15
   23    18        INIT_FCALL                                               'array_search'
         19        SEND_VAR                                                 !6
         20        SEND_VAR                                                 !3
         21        SEND_VAL                                                 <true>
         22        DO_ICALL                                         $17     
         23        TYPE_CHECK                                  1018          $17
         24      > JMPZ                                                     ~18, ->31
   24    25    >   INIT_FCALL                                               'array_search'
         26        SEND_VAR                                                 !6
         27        SEND_VAR                                                 !3
         28        DO_ICALL                                         $19     
         29        FETCH_DIM_R                                      ~20     !4, $19
         30        ASSIGN                                                   !6, ~20
   27    31    >   TYPE_CHECK                                  128          !5
         32      > JMPZ                                                     ~22, ->38
   28    33    >   INIT_FCALL_BY_NAME                                       'array_replace_keys'
         34        SEND_VAR_EX                                              !5
         35        SEND_VAR_EX                                              !1
         36        DO_FCALL                                      0  $23     
         37        ASSIGN                                                   !5, $23
   31    38    >   ASSIGN_DIM                                               !2, !6
         39        OP_DATA                                                  !5
   22    40      > JMP                                                      ->16
         41    >   FE_FREE                                                  $14
   34    42      > RETURN                                                   !2
   35    43*     > RETURN                                                   null

End of function array_replace_keys

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
139.72 ms | 1008 KiB | 18 Q