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] =&开发者_开发知识库gt; 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!
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.
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();
精彩评论