'How to target another value of the same object?

I have an object:

{
    "id": "GRP-25",
    "text": "All Areas",
    "groupid": 0,
    "leaf": false,
    "qtip": "Common",
    "link": null,
    "helplink": null,
},

Here I'm changing the value of ID but I'm trying to change it only if leaf is true. How do I target the value of a leaf into the if statement?

array_walk_recursive($result, function (&$v, $k) { 
    if($k == 'id'){ 
        $v ='GRP-'.$v; 
    }    
});


Solution 1:[1]

I don't think you will be able to do that with array walk, I would be inclined to use a foreach

something along these lines

foreach ($results as &$result) {
  if ($result->leaf == true) {
    $result->id = 'GRP-'.$result->id; 
  }
}

Assuming here you did json_decode as objects, otherwise if you are doing json_decode as arrays, then you will need to change the -> to []

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bumperbox