I am needing to use an XML based API, for something i am working on. I decided because this XML needs to be very dynamic it would be easier to work in arrays and finally convert back to XML. I am trying to use the top voted solution here How to convert array to SimpleXML to do this, which is all well and good when i use a normal array. However i am using another API to pull down the data, and that data is in the form of a SOAP object. like this:
Array
(
[0] => Soap_1_0_Invoice Object
(
[items] => Array
(
[0] => _Soap_1_0_Item Object
(
I have tried casting this to an array, and i've done gettype() at every point to make sure that this is an array, and infact it is. However the contents of it still say "Object" when i do print_r.
I am using the function provided in this answer which is:
function array_to_xml( $data, &$xml_data ) {
foreach( $data as $key => $value ) {
if( is_numeric($key) ){
$key = 'item'.$key; //dealing with <0/>..<n/> issues
}
if( is_array($value) ) {
$subnode = $xml_data->addChild($key);
array_to_xml($value, $subnode);
} else {
$xml_data->addChild("$key",htmlspecialchars("$value"));
}
}
}
When it hits
$xml_data->addChild("$key",htmlspecialchars("$value"));
I get an error saying that this object cannot be converted to a string at this line. Is it possible to convert this to an array which will be usable, or have i had a fundamental misunderstanding of what is going on here?