1

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!

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

i don't see why [photo] => Array doesn't indicate an array. nevertheless, most simple and useful answer i can imagine +1 :)
@Samuel Herzog That's an attribute of the object $xml->photos;, but you're right, that is an array; but at the wrong place.
1

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();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.