2

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.

2
  • 2
    there isn't a built-in shortcut syntax for this. you'd have to 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). Commented Jul 24, 2015 at 15:09
  • possible duplicate of Simplexml get path from variable Commented Jul 24, 2015 at 15:34

2 Answers 2

3

You can't include pointers in the variable variable.

$members = explode('->', $path);
foreach($xml->{$members[0]}->{$members[1]} as $v) {

}

This will only work if your path stays two dimensional.
[edit] If you do need it to work recursively you can use this. Sorry took me a minute to write it:

function pathFinder($path, $obj) {
    $members = explode('->', $path);

    if(is_object($obj) && count($members) && property_exists($obj, $members[0])) {
        $nextMember = $members[0];
        array_shift($members);
        return pathFinder(implode('->', $members), $obj->{$nextMember});
    } else {
        return $obj;
    }
}

$xml = new stdClass;
$xml->channel->item[] = 'love';
$xml->channel->item[] = 'test';

$path = 'channel->item';
$array = pathFinder($path, $xml);
print_r($array);

output:

Array(
    [0] => love
    [1] => test
)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, really appreciate this - it works perfectly. In the first code you entered the $path[0] in curly braces. The code works fine with or without them so can you tell me what their purpose is please? I've never used curly braces like that before in PHP
Curly braces are just a standard I use when using variables for properties. It's safer like using parenthesis for precedent protection, even though they aren't always required. If you think about the literal here it's $xml->'channel', when the variable is used, which doesn't work but $xml->{'channel'} does.
2

Try something like this:

#Object for test
$xml = new stdClass();
$xml->channel->item[] = "banana";
$xml->channel->item[] = "abacate";
$xml->channel->item[] = "manga";
$xml->channel->item[] = "abacaxi";
$xml->channel->item[] = "morango";

list($channel, $item)= explode('->','channel->item');

foreach ($xml->{$channel}->{$item} as $newsItem) :
    var_dump($newsItem);
endforeach;

1 Comment

This code would only ever be useful if you knew that there would be exactly two dimensions, but not what those dimensions were called. That seems kind of unlikely to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.