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);
2
  • 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'); Commented Jan 19, 2018 at 1:39
  • super nice thank you! Commented Jan 19, 2018 at 2:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.