SimpleXML 和封闭数组的问题

发布于 2024-09-29 09:10:03 字数 595 浏览 0 评论 0原文

我在使用 SIMPLE XML 时遇到问题。我似乎有一个转储整个对象的数组。但是,当我尝试访问该数组时,我得到了该数组的单个元素。

这是完整的转储:

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

    [person] => Array
        (
            [0] => SimpleXMLElement Object
                (
                 ..........................
                ),
            [1] => SimpleXMLElement Object
                (
                 ..........................
                )
        )
)

当我尝试通过 $xml->person 访问 person 数组时,我没有获取数组,而是取回了第一个元素。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2024-10-06 09:10:03

来自 PHP 手册中的 SimpleXML 基本用法文档:

注意:属性(前面示例中的$xml->movie)不是数组。它们是可迭代可访问对象。

因此,在您的情况下, $xml->person 实际上并不是一个数组,只是一个可迭代的对象。它可以很容易地转换为数组:

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文