Here is my code:
$string = <<<XML
<?xml version='1.0'?>
<test>
<testing>
<lol>hello</lol>
<lol>there</lol>
</testing>
</test>
XML;
$xml = simplexml_load_string($string);
echo "All of the XML:\n";
print_r $xml;
echo "\n\nJust the 'lol' array:";
print_r $xml->testing->lol;
Output:
All of the XML:
SimpleXMLElement Object
(
[testing] => SimpleXMLElement Object
(
[lol] => Array
(
[0] => hello
[1] => there
)
)
)
Just the 'lol' array:
SimpleXMLElement Object
(
[0] => hello
)
Why does it output only the [0] instead of the whole array? I dont get it.
It’s because you have two lol elements. In order to access the second you need to do this:
this will give you “there”
Will give you “hello”
The children() method of the SimpleXMLElement will give you an object containing all the children of an element for example:
will give you an object containing all the children of the “testing” SimpleXMLElement.
If you need to iterate, you can use the following code:
There is more information about SimpleXMLElement here:
http://www.php.net/manual/en/class.simplexmlelement.php