Jump to content

get values from object


Lassie

Recommended Posts

I have a SimpleXML Object and want to get the values. I have made some progress but cant work out how to iterate through the sub array values.

This is the object and I want to be able to access all the  values in the sub array [results].

Array
(
    [area_name] =>  SE12
    [bounding_box] => SimpleXMLElement Object
        (
            [latitude_max] => 51.458974
            [latitude_min] => 51.425854
            [longitude_max] => 0.052713
            [longitude_min] => 0.003556
        )

    [country] => England
    [county] => London
    [latitude] => 51.442414
    [longitude] => 0.0281345
    [postcode] => SE12
    [result_count] => 361
    [results] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [name] => SE12 0JB
                    [latitude] => 51.442354
                    [longitude] => 0.016964
                    [zed_index] => 447549
                )

            [1] => SimpleXMLElement Object
                (
                    [name] => SE12 0BZ
                    [latitude] => 51.433664
                    [longitude] => 0.022869
                    [zed_index] => 445747
                )

            [2] => SimpleXMLElement Object
                (
                    [name] => SE12 0LF
                    [latitude] => 51.437281
                    [longitude] => 0.021230
                    [zed_index] => 445634
                )

            [3] => SimpleXMLElement Object
                (
                    [name] => SE12 9LG
                    [latitude] => 51.446778
                    [longitude] => 0.018570
                    [zed_index] => 444432
                )
etc

I can access the first values with this

$xml = new SimpleXMLElement($curl_response);
                            $array = (array) $xml;
                            //get values
                            $area_name=$xml->area_name;
                            echo $area_name .'<br/>';
//access [results]
                            $name= $xml[results][0]->name;
                            echo $name;
This fails.

I would like to iterate through all the numeric indexes and get the values to store in a database.

I think i need a foreach and to assign the values to as string but cant get the construct right,

Any help appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/296107-get-values-from-object/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.