Lassie Posted May 6, 2015 Share Posted May 6, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296107-get-values-from-object/ Share on other sites More sharing options...
CroNiX Posted May 6, 2015 Share Posted May 6, 2015 (edited) 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 May 6, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/296107-get-values-from-object/#findComment-1510917 Share on other sites More sharing options...
Lassie Posted May 6, 2015 Author Share Posted May 6, 2015 Right. Thanks very much. If I want to store in a database do I need to use string? Quote Link to comment https://forums.phpfreaks.com/topic/296107-get-values-from-object/#findComment-1510920 Share on other sites More sharing options...
Muddy_Funster Posted May 6, 2015 Share Posted May 6, 2015 ... If I want to store in a database do I need to use string? Could you clarify what you are asking here? Quote Link to comment https://forums.phpfreaks.com/topic/296107-get-values-from-object/#findComment-1510923 Share on other sites More sharing options...
Lassie Posted May 7, 2015 Author Share Posted May 7, 2015 Hi, I have answered my own question.Thank you for your help. Topic is solved. Quote Link to comment https://forums.phpfreaks.com/topic/296107-get-values-from-object/#findComment-1510997 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.