我确信这是一个简单的问题。我在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
)
)
)
)
谢谢你的帮助!
答案 0 :(得分:3)
我在示例中看不到数组。但是,$xml
是可以遍历的,所以你可能就是这个意思。 $xml->photos
仅选择第一个 photo
元素。你可能正在寻找
$photosArray = $xml->xpath('//photo');
确实会返回一个数组。
答案 1 :(得分:1)
要返回所有照片,可以在children()
您可以将simplexml对象列表转换为数组,例如
$photosArray = (array)$xml->children();
/* or retain the simplexml object */
$photosArray = $xml->children();