3v4l.org

run code in 300+ PHP versions simultaneously
<?php function custom($arr, $newKey, $oldKey) { // if the value is not an array, then you have reached the deepest // point of the branch, so return the value if (!is_array($arr)) { return $arr; } $result = []; // empty array to hold copy of subject foreach ($arr as $key => $value) { // replace the key with the new key only if it is the old key $key = ($key === $oldKey) ? $newKey : $key; // add the value with the recursive call $result[$key] = custom($value, $newKey, $oldKey); } return $result; } $var = '[{"player":"Tiger Woods"},{"player":"Gary Player"}]'; $temp = json_decode($var, true); $temp = custom($temp, 'golfer','player'); print_r($temp);
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/pcFEj
function name:  (null)
number of ops:  16
compiled vars:  !0 = $var, !1 = $temp
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   19     0  E >   ASSIGN                                                   !0, '%5B%7B%22player%22%3A%22Tiger+Woods%22%7D%2C%7B%22player%22%3A%22Gary+Player%22%7D%5D'
   20     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $3      
          5        ASSIGN                                                   !1, $3
   21     6        INIT_FCALL                                               'custom'
          7        SEND_VAR                                                 !1
          8        SEND_VAL                                                 'golfer'
          9        SEND_VAL                                                 'player'
         10        DO_FCALL                                      0  $5      
         11        ASSIGN                                                   !1, $5
   22    12        INIT_FCALL                                               'print_r'
         13        SEND_VAR                                                 !1
         14        DO_ICALL                                                 
         15      > RETURN                                                   1

Function custom:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 6, Position 2 = 7
Branch analysis from position: 6
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 25
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 25
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 13, Position 2 = 15
Branch analysis from position: 13
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 25
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 25
filename:       /in/pcFEj
function name:  custom
number of ops:  28
compiled vars:  !0 = $arr, !1 = $newKey, !2 = $oldKey, !3 = $result, !4 = $value, !5 = $key
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
    2     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
    6     3        TYPE_CHECK                                  128  ~6      !0
          4        BOOL_NOT                                         ~7      ~6
          5      > JMPZ                                                     ~7, ->7
    7     6    > > RETURN                                                   !0
   10     7    >   ASSIGN                                                   !3, <array>
   11     8      > FE_RESET_R                                       $9      !0, ->25
          9    > > FE_FETCH_R                                       ~10     $9, !4, ->25
         10    >   ASSIGN                                                   !5, ~10
   13    11        IS_IDENTICAL                                             !5, !2
         12      > JMPZ                                                     ~12, ->15
         13    >   QM_ASSIGN                                        ~13     !1
         14      > JMP                                                      ->16
         15    >   QM_ASSIGN                                        ~13     !5
         16    >   ASSIGN                                                   !5, ~13
   15    17        INIT_FCALL_BY_NAME                                       'custom'
         18        SEND_VAR_EX                                              !4
         19        SEND_VAR_EX                                              !1
         20        SEND_VAR_EX                                              !2
         21        DO_FCALL                                      0  $16     
         22        ASSIGN_DIM                                               !3, !5
         23        OP_DATA                                                  $16
   11    24      > JMP                                                      ->9
         25    >   FE_FREE                                                  $9
   17    26      > RETURN                                                   !3
   18    27*     > RETURN                                                   null

End of function custom

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
141.06 ms | 1013 KiB | 16 Q