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
Share on other sites

Because you're trying to access an array element instead of the object.

 

foreach($xml->results as $result) //cycle through results object
{
  //access the properties of this $result object
  echo $result->name;
  echo $result->longitute;
}
Edited by CroNiX
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.