0
votes

I have an xml document which I am trying to access a value of a certain child element using xpath.

$xml = new SimpleXMLElement($url,null,true);
$value = $xml->xpath("/xxx/yyy/zzz");

when I use:

print_r($value);

ouputs the following:

Array
(
    [0] => SimpleXMLElement Object
        (
            [0] => 1003.39
        )

)

I can access the value of the SimpleXMLElement Object by doing the following:

echo $value[0]

however I don't want to store the SimpleXMLElement Object within $value. I want to store the objects value within $value.

e.g. I want to use:

echo $value

to return

1003.39

What do I need to change in

$value = $xml->xpath("/xxx/yyy/zzz");

in order to return the value, not the object?

p.s. If I cast to a string using (string), then my print_r or echo $value prints "Array" instead of the value!

1

1 Answers

1
votes

->xpath() returns an array by definition, so:

$result = $xml->xpath("/xxx/yyy/zzz");
$value = $result[0];

ANd then either:

$value = (string)$value;

Or:

$value = strval($value);

If you want the whole array cast to string:

$result = $xml->xpath("/xxx/yyy/zzz");
$result = array_map('strval',$result);