Question

After calling the prestashop webservice, I recieved the response as shown below:

SimpleXMLElement Object
(
    [order] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 1
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [id] => 2
                        )

                )

        )

)

I tried to retrive the content by looping as shown below:

foreach($resources as $resource){
    echo '<pre>';
    print_r($resource['id']);
    echo '</pre>';
}

This gives me:

SimpleXMLElement Object
(
    [0] => 1
)
SimpleXMLElement Object
(
    [0] => 2
)

How can I retrieve 1 and 2 which are the values of these objects? thanks

Was it helpful?

Solution

I hate simplexml...

<?php
$xml = file_get_contents('xml.xml');
$xml = new SimpleXMLElement($xml);

foreach($xml as $key => $value)
{
        $attrs = $value->attributes();
        foreach($attrs as $attr_k => $attr_v)
                echo $attr_k.": ".$attr_v."\n";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top