将数组分配给变量时出现问题

发布于 2024-10-11 02:48:51 字数 3134 浏览 1 评论 0原文

我确信这是一个简单的问题。我在 simplexml 对象中有一个数组。当我尝试将数组分配给变量时,它仅分配数组的第一个索引。我怎样才能让它分配整个数组。这是我的代码。

$xml = simplexml_load_string(FlickrUtils::getMyPhotos("flickr.photos.search", $_SESSION['token']));

$photosArray = $xml->photos;
//$photosArray = $xml->photos->photo;

//echo gettype($photosArray);
print_r($photosArray);

这是 print_r($photosArray); 的结果

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [page] => 1
            [pages] => 1
            [perpage] => 100
            [total] => 4
        )

    [photo] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 5335626037
                            [owner] => 57991585@N02
                            [secret] => bd66f06b49
                            [server] => 5210
                            [farm] => 6
                            [title] => 1
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 5336238676
                            [owner] => 57991585@N02
                            [secret] => 898dffa011
                            [server] => 5286
                            [farm] => 6
                            [title] => 2
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 5335625381
                            [owner] => 57991585@N02
                            [secret] => 60a0c84597
                            [server] => 5126
                            [farm] => 6
                            [title] => 4
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 5335625195
                            [owner] => 57991585@N02
                            [secret] => 49348c1e8b
                            [server] => 5126
                            [farm] => 6
                            [title] => 3
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                )

        )

)

谢谢你的帮助!

I'm sure this is a simple one. I have an array in a simplexml object. When I try to assign the array to a variable, it only assigns the first index of the array. How can I get it to assign the whole array. This is my code.

$xml = simplexml_load_string(FlickrUtils::getMyPhotos("flickr.photos.search", $_SESSION['token']));

$photosArray = $xml->photos;
//$photosArray = $xml->photos->photo;

//echo gettype($photosArray);
print_r($photosArray);

This is the result of the print_r($photosArray);

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [page] => 1
            [pages] => 1
            [perpage] => 100
            [total] => 4
        )

    [photo] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 5335626037
                            [owner] => 57991585@N02
                            [secret] => bd66f06b49
                            [server] => 5210
                            [farm] => 6
                            [title] => 1
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 5336238676
                            [owner] => 57991585@N02
                            [secret] => 898dffa011
                            [server] => 5286
                            [farm] => 6
                            [title] => 2
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 5335625381
                            [owner] => 57991585@N02
                            [secret] => 60a0c84597
                            [server] => 5126
                            [farm] => 6
                            [title] => 4
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 5335625195
                            [owner] => 57991585@N02
                            [secret] => 49348c1e8b
                            [server] => 5126
                            [farm] => 6
                            [title] => 3
                            [ispublic] => 1
                            [isfriend] => 0
                            [isfamily] => 0
                        )

                )

        )

)

Thanks for youe help!

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

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

发布评论

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

评论(2

冷夜 2024-10-18 02:48:51

我在你的例子中没有看到数组。但是, $xml 是可遍历的,所以您可能是这个意思。但 $xml->photos 仅选择 first photo 元素。您可能正在寻找

$photosArray = $xml->xpath('//photo');

哪个确实返回一个数组。

I fail to see an array in your example. However, $xml is traversable, so you probably mean that. $xml->photos selects only the first photo element though. You are probably looking for

$photosArray = $xml->xpath('//photo');

which indeed returns an array.

£冰雨忧蓝° 2024-10-18 02:48:51

为了返回所有照片,可以在 children() 上使用

您可以将 simplexml 对象列表转换为数组,例如

$photosArray = (array)$xml->children();

/* or retain the simplexml object */
$photosArray = $xml->children();

In order to return all photo, can make used on children()

You can cast the list of simplexml objects into array, like

$photosArray = (array)$xml->children();

/* or retain the simplexml object */
$photosArray = $xml->children();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文