I\'ve to implement a setter in PHP, that allows me to specify the key, or sub key, of an array (the target), passing the name as a dot-separated-keys value.
If the keys of the array are unique, you can solve the problem in a few lines of code using array_walk_recursive:
$arr = array('a' => 1,
'b' => array(
'y' => 2,
'x' => array('z' => 5, 'w' => 'abc')
),
'c' => null);
function changeVal(&$v, $key, $mydata) {
if($key == $mydata[0]) {
$v = $mydata[1];
}
}
$key = 'z';
$value = '56';
array_walk_recursive($arr, 'changeVal', array($key, $value));
print_r($arr);