0

I can't figure this one out:

$xml = simplexml_load_string($result);

This results in the following (which I can see by view>source in Chrome):

(
[option] => Array
    (
        [0] => abc1234
        [1] => def11234
        [2] => ghi1234
        [3] => jkl1234           
    )
[items] => SimpleXMLElement Object
    (
        [@attributes] => Array
            (
                [total] => 300
            )
        [item] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [name] => NAME1
                            )
                        [urls] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [url] => example.com
                                        [img] => /image1.png
                                    )
                            )
                    )
                [1] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [name] => NAME2
                            )

                        [urls] => SimpleXMLElement Object
                            (
                                [@attributes] => Array
                                    (
                                        [url] => example2.com
                                        [img] => /image2.png
                                    )
                            )                          
                    )
            )
    )
)

This is part of an array with thousands of results. The data that I need are in the 'name,' 'url' and 'image' fields. I have searched tons but the only thing that works is extracting the 'option' values which I don't even need.

How do I extract these values? Thank you

2 Answers 2

1

The answer came down to syntax:

foreach($xml->items->item as $items) {

    print $items->attributes()->name.'<br>';

}

I did not realize that 'attributes' was part of the node that needed to be named and that you need to call it as 'attributes().' Live and learn.

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

Comments

0

The easiest way, that I know of, to loop over your elements is just a foreach loop:

foreach($xml as $item)

Then, just look at the datatype of each element. If it's some sort of assoc array, access with a key, if it's a SimpleXMLObject, access the element as an object property.

Granted, this can depend on how deeply nested your XML is, and how much you have. Some XSL might save you some trouble too.

7 Comments

I tried that exact 'foreach.' However, it only returns items in the first level under 'option.' How do I get the 'foreach' to return the options lower down? Thanks for your help so far!
You would need to nest the foreach loop in that case. How deep does the rabbit hole go? You may need to use some other XML parser depending on what data you're trying to get to.
It doesn't go any deeper than the snippet I posted above
Ok, nesting another foreach loop wouldn't be too bad then. If you notice performance issues try another way to parse your XML. SimpleXML is awesome at being simple, but it's not the fastest guy in town.
Forgive my ignorance, but how would I structure nested loops here? The data that I need is not a level deep in the XML, its just not the first level. So, I cannot loop within the first results, rather I need the layer below the second result which is items>item>name. I'm not sure if I'm making sense.
|

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.