简体   繁体   中英

problem with SimpleXML and inclosed arrays

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] => SimpleXMLElement 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM