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);
Output for git.master, git.master_jit, rfc.property-hooks
Array ( [0] => Array ( [golfer] => Tiger Woods ) [1] => Array ( [golfer] => Gary Player ) )

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
82.49 ms | 405 KiB | 5 Q