3v4l.org

run code in 500+ PHP versions simultaneously
<?php $result = json_decode('[ { "id": "GRP-25", "text": "All Areas", "groupid": 0, "leaf": false, "qtip": "Common", "link": null, "helplink": null, "children": [ { "id": "GRP-25", "text": "All Areas", "groupid": 0, "leaf": true, "qtip": "Common", "link": null, "helplink": null } ] }, { "id": "GRP-25", "text": "All Areas", "groupid": 0, "leaf": false, "qtip": "Common", "link": null, "helplink": null } ]', true); function changeLeafId(array &$data) { foreach ($data as $k => $v) { // recursive call if (is_array($v)) { changeLeafId($data[$k]); continue; } // update 'id' from 'leaf' if ($k == 'id' && $data['leaf'] ?? false) { $data[$k] = 'GRP-' . $v; } } } changeLeafId($result); print_r($result);
Output for 7.4.0 - 7.4.33, 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.21, 8.5.0 - 8.5.6
Array ( [0] => Array ( [id] => GRP-25 [text] => All Areas [groupid] => 0 [leaf] => [qtip] => Common [link] => [helplink] => [children] => Array ( [0] => Array ( [id] => GRP-GRP-25 [text] => All Areas [groupid] => 0 [leaf] => 1 [qtip] => Common [link] => [helplink] => ) ) ) [1] => Array ( [id] => GRP-25 [text] => All Areas [groupid] => 0 [leaf] => [qtip] => Common [link] => [helplink] => ) )

preferences:
102.73 ms | 1427 KiB | 4 Q