Let's say that we want to add new data ($newData) to an multi-dimensional array ($array) using an other array as path ($path) that can be of any size.
$array = json_decode(file_get_contents('data.json'), true);
$path = [0,1,1]; # or bigger
...
array_push($array[0]['children'][1]['children'][1]['children'], $newData);
file_put_contents('data.json', json_encode($array, JSON_PRETTY_PRINT));
How to translate
$path = [0,1,1]; # or bigger
to the corresponding
array_push($array[0]['children'][1]['children'][1]['children'], $newData);
function setDeep(&$le_array, $path, $value) { $tempArr = &$le_array; foreach($path as $key) { $tempArr = &$tempArr[$key]['children']; } array_push($tempArr,$value); } setDeep($array, [0,1,1], 'hello');