0

I have an array

$animals['dogs']['lassie']['legs'] = 4

Given the variables

$path  = "['dogs']['lassie']['legs']";
$value = 3;

How do I update the $animals array so that

$animals['dogs']['lassie']['legs'] = 3;

My best guess of

$arr = 'animals'.$path;
${$arr} = $value;

didn't work.

I have read that it can be done using eval() but also that it shouldn't be done using eval()!

Any help is gratefully received.

Jules
  • 275
  • 1
  • 4
  • 11
  • 1
    Are you fixed with the format of `$path`, something like "dogs.lassie.legs" would be easier (you could then use https://stackoverflow.com/questions/9628176/using-a-string-path-to-set-nested-array-data) – Nigel Ren Nov 12 '19 at 18:20
  • That would work. Thank you very much – Jules Nov 12 '19 at 18:34

0 Answers0