I’m using SimpleXML to parse an XML API.
The API returns several child elements like this:
...
<Rank0>Something</Rank0>
<Rank1>Something</Rank1>
<Rank2>Something</Rank2>
...
When to access <Rank1>, for example, I can do the following:
$response->Result->Rank1;
However, I need to loop through these values dynamically. In pseudocode, something like this:
foreach($response->Result>Rank*){
echo "looping through rank" . $number;
echo "value is" . $value;
}
How do I do this?
Thanks for any help in advance.
Use
SimpleXMLElement::children():If you need to verify that the node name is in the form of “Name” + number, you can add a condition inside your loop like the following: