将数组分配给变量时出现问题
我确信这是一个简单的问题。我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在你的例子中没有看到数组。但是,
$xml
是可遍历的,所以您可能是这个意思。但$xml->photos
仅选择 firstphoto
元素。您可能正在寻找哪个确实返回一个数组。
I fail to see an array in your example. However,
$xml
is traversable, so you probably mean that.$xml->photos
selects only the firstphoto
element though. You are probably looking forwhich indeed returns an array.
为了返回所有照片,可以在
children()
上使用您可以将 simplexml 对象列表转换为数组,例如
In order to return all photo, can make used on
children()
You can cast the list of simplexml objects into array, like