0

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);
grrr
  • 151
  • 11
  • Since I was already in the midst of doing it: `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');` – AFwcxx Jan 19 '18 at 01:39
  • super nice thank you! – grrr Jan 19 '18 at 02:22

0 Answers0