0

Let's suppose that I have the next property available:

$object->nestedArray[0]->prop

I want to access that variable dinamically trough a property like this one:

$prop = 'nestedArray[0]->prop';
var_dump($object->$prop);

But this is not valid, altough it would be valid if $prop were 'nestedArray'

How can I achieve this? I need to make $prop a valid access (or get a way around).

Here is a live example where you can play: http://ideone.com/vUbSSf

Felix
  • 579
  • 1
  • 8
  • 23
  • Just quickly, you could use ( print 'Magic Route1:'; var_dump($object->{$route1}); ) but that doesnt work for route2 or route3 as is – Daniel Brose Jul 23 '15 at 03:28
  • I'm loathe to say it (and that's why this is a comment and not an answer) but you could use `eval` ~ `eval("return \$object->${route3};")` – Phil Jul 23 '15 at 03:34
  • @Phil, i started playing with that myself, then found this: http://stackoverflow.com/questions/2036547/variable-variables-pointing-to-arrays-or-nested-objects , i dont know how to flag post as 'already answered' or whatever, but that looks like the answer to me. – Daniel Brose Jul 23 '15 at 03:36
  • @DanielB what about $prop = 'nestedArray[0]->nestedNested[1]->prop'; ? – Felix Jul 23 '15 at 12:19
  • I tried that and other variations in the ideone before googling to find existing solution. Using {$x} when $x includes any indexes never worked, eval did but i dont like eval and also not sure i knew best way using eval to avoid pitfalls, so googled and found the duplicate. – Daniel Brose Jul 24 '15 at 02:35

0 Answers0