I have the following XML saved in $string
<?xml version="1.0" encoding="ISO-8859-1"?>
<users>
<user id="1">
<name>Michael Tray</name>
<account>473.43</account>
</user>
<user id="2">
<name>Sandra Marry</name>
<account>154.24</account>
</user>
</users>
I use the following simple XPath expression to get all names
$xml = simplexml_load_string($string);
$result = $xml->xpath("/users/user/name");
echo "<pre>";
print_r($result);
echo "</pre>";
What I get
Array (
[0] => SimpleXMLElement Object
(
[0] => Michael Tray
)
[1] => SimpleXMLElement Object
(
[0] => Sandra Marry
)
)
What I want
Array (
[0] => SimpleXMLElement Object
(
[name] => Michael Tray
)
[1] => SimpleXMLElement Object
(
[name] => Sandra Marry
)
)
So the SimpleXMLElement key should be a string (tag name) and not a number. How can I do that?