Accessing data in object inside array using php's SimpleXML
This is related to my earlier question here: Trying to read XML nodes using PHP DOM
I have an array which has elements that look like this:
[rpc.pingomatic.com] => Array
(
[http://www.example.com] => Array
(
[0] => SimpleXMLElement Object
(
[name] => flerror
[value] => SimpleXMLElement Object
(
[boolean] => 0
)
)
[1] => SimpleXMLElement Object
(
[name] => message
[value] => SimpleXMLElement Object
(
[string] => Pings being forwarded to 11 services!
)
)
开发者_如何学Go )
I want to retrieve the data and finally display it in this manner:
flerror:0 Message from directory:Pings being forwarded to 11 servicesI can use this to retrieve the necessary fields:
echo $response->name;
echo $response->value->boolean;
echo $response->value->string;
However, I am using a foreach loop to traverse through the array, and not all elements have the fields named boolean and string. They might have something like flag instead of boolean, etc.
Is there a generic way to retrieve the necessary data using the same foreach loop?
You could use this method PHP: SimpleXMLElement::xpath - Manual
I figured the best way to deal with my problem is to use
isset($response->value->boolean)
or isset($response->value->string)
to ascertain the type of the value and echo it out.
精彩评论