$array = [
'a' => [
'b' => [
'c' => [
'd' => 'text'
]
]
]
];
function set_value($indexes, &$array, $data)
{
if(count($indexes) > 1){
set_value(array_slice($indexes, 1), $array[$indexes[0]], $data);
}else{
$array[$indexes[0]] = $data;
}
}
set_value(['a', 'b', 'c', 'd'], $array, 'TEST DATA');
function setValue($value, $key, array &$arr) {
foreach ($arr as $arrKey=>&$arrValue) {
if ($arrKey == $key) {
$arrValue = $value;
} elseif (is_array($arrValue)) {
setValue($value, $key, $arrValue);
} else {
throw new \InvalidArgumentException("Key {$key} not found in array");
}
}
return $arr;
}
// Check
$Arr['key1']['key2']['key3']['key4']['key5']['keyn'] = null;
setValue('Test Value', "keyn", $Arr);
var_dump($Arr);
/*
array(1) {
["key1"]=>
array(1) {
["key2"]=>
array(1) {
["key3"]=>
array(1) {
["key4"]=>
array(1) {
["key5"]=>
array(1) {
["keyn"]=>
string(10) "Test Value"
}
}
}
}
}
}
*/