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

5
  • Just quickly, you could use ( print 'Magic Route1:'; var_dump($object->{$route1}); ) but that doesnt work for route2 or route3 as is Commented Jul 23, 2015 at 3: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};") Commented Jul 23, 2015 at 3:34
  • @Phil, i started playing with that myself, then found this: stackoverflow.com/questions/2036547/… , i dont know how to flag post as 'already answered' or whatever, but that looks like the answer to me. Commented Jul 23, 2015 at 3:36
  • @DanielB what about $prop = 'nestedArray[0]->nestedNested[1]->prop'; ? Commented Jul 23, 2015 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. Commented Jul 24, 2015 at 2:35

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.