XML : SimpleXML addChild() adding child node titles not values

When attempting to use SimpleXML's addChild() method towards the end of the code block below and printing the feed, the output is not the required result. When tested with a string value, addChild() correctly outputs both the name of the node and the value, however I'm not sure if the array or the object inside it are actually within the scope of the first 'foreach' loop.

I want to access the 'name' and 'type' properties of the $nameType object and output them to my main feed with addChild().

  //Turns base feed into SimpleXML object.    $feed = simplexml_load_string($xml);  foreach($feed->property as $property) {    //Gets latitude and longitude from each property.      $latitude = $property->latitude;      $longitude = $property->longitude;    //Adds latitude and longitude values into Google Places API URL.      $googleURL = 'https://maps.googleapis.com/maps/api/place/nearbysearch/xml?location='.$latitude.','.$longitude.'&radius=1000&types=train_station&key=my-google-key';    //Gets XML from Google Places and parses into SimpleXML Object.      $googleXMLfile = file_get_contents($googleURL);      $googleXMLdata = simplexml_load_string($googleXMLfile);    //Array for limiting number of results.      $googleStoredXMLDataArray = array();    foreach ($googleXMLdata->result as $result) {        //Assigns result 'name' and 'type' to variables.          $name = $result->name;          $type = $result->type;        //Creates object to store name and type together.          $nameType = new StdClass();          $nameType->name = $name;          $nameType->type = $type;        //Pushes object to array and outputs results limiting feed to 3 results.          array_push($googleStoredXMLDataArray, $nameType);          $output = array_slice($googleStoredXMLDataArray, 0, 3);      }    //Adding proximityTo destination to property nodes in the feed parser.  //Error - needs to pull properties from object inside the array.      $property->addChild('proximityTo_name', '$googleStoredXMLDataArray->$nameType->name');      $property->addChild('proximityTo_type', '$googleStoredXMLDataArray->$nameType->type');  }    print_r($feed);    

No comments:

Post a Comment