I have 4 SimpleXMLElement objects that get output from within a foreach loop?
I need to combine the 4 objects into 1 object before outputting to xml. This is what I'm stuck on.
$auth_tokens = array('tok1', 'tok2', 'tok3', 'tok4');
foreach($auth_tokens as $auth_token) { // 4 iterations in loop
$response = curl_exec($connection); // API xml response
$xml = simplexml_load_string($response); // loaded xml into object
$entries = $xml->PaginationResult->TotalNumberOfEntries;
$xml = $xml->OrderArray->Order; // Only want this section of each object
if($entries == 0) {
continue;
}
echo '<pre>' . var_export($xml, true) . '</pre><br>';
}
Output: condensed version of objects
SimpleXMLElement::__set_state(array(
'OrderID' => '1118',
));
SimpleXMLElement::__set_state(array(
'OrderID' => '3916',
));
SimpleXMLElement::__set_state(array(
'OrderID' => '1628',
));
SimpleXMLElement::__set_state(array(
'OrderID' => '3724',
));
After the 4 objects are combined, I will echo $xml->asXml() outside the loop and be able to add my parent node.
foreach($auth_tokens as $auth_token) {
// ....
}
$xml = simplexml_load_string("<Orders>$xml</Orders>");
header('content-type: text/xml');
echo $xml->asXML();
How can I get those 4 objects merged into 1 and not have to do anymore loops to make it happen?