开发者

problem with SimpleXML and inclosed arrays

开发者 https://www.devze.com 2023-01-22 03:28 出处:网络
I\'m having a problem with SIMPLE XML.I seem to have an array with dumping the whole object.However, when I try to access the array, I get a single element of the array back.

I'm having a problem with SIMPLE XML. I seem to have an array with dumping the whole object. However, when I try to access the array, I get a single element of the array back.

Here is the full dump:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => array
        )

    [person] => Array
        (
            [0] => SimpleX开发者_高级运维MLElement Object
                (
                 ..........................
                ),
            [1] => SimpleXMLElement Object
                (
                 ..........................
                )
        )
)

when I try to access the person array through $xml->person, instead of getting the array, I get the first element back. Any ideas?


From the SimpleXML Basic usage documentation in the PHP Manual:

NOTE: Properties ($xml->movie in previous example) are not arrays. They are iterable and accessible objects.

So, in your case, $xml->person is not actually an array, just an iterable object. It can be easily converted to an array with:

$persons = array();
foreach ($xml->person as $person) {
    $persons[] = $person;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号