I'm trying to use a variable in a foreach loop (with simplexml) but am confused as to how to get it work properly.
My variable is:
$path="channel->item";
And I want my foreach to look like:
foreach ($xml->".$path." as $newsItem)
But that doesn't work - like the $path is being echo as $path rather than it's contents.
if I do:
foreach ($xml->channel->item as $newsItem)
It works fine.
I'm sure it's just a syntax issue but I can't figure it out. Thanks in advance.
explode()your path on->, then loop over each individual item and "dig" into your xml. that or you could just use a proper xpath-type path instead of->, in which case you'd just$xpath->query($path).