0

I've started parsing XML with PHP and got stuck with this problem. I've managed to query the XML with the following method:

echo $xml->textbox->item->id;

What's the syntax to use a variable instead of textbox?

I've tried (but didn't work):

echo $xml[$type]->item->id;
echo $xml->[$type]->item->id;

Any ideas?

1 Answer 1

2
$xml->{$variable}->item->id

will work. You may want to check first whether that path exists, e.g. using empty().

Sign up to request clarification or add additional context in comments.

2 Comments

thanks - this works! i forgot to ask something else: how to put the variable inside the for each loop? foreach($xml as {$type]) doesnt work .. :(
@Fuxi to be honest, I'm not entirely sure: XML nodes have their own structure. Try to see what comes out of foreach ($xml as $key => $value) - see what $key and $value contain

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.