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);

preferences:
102.26 ms | 1426 KiB | 5 Q