0

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?

heidi sievert
  • 69
  • 1
  • 5
  • Possible duplicate of [What is the difference between single-quoted and double-quoted strings in PHP?](https://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – tyteen4a03 Nov 13 '17 at 16:16
  • What are `$key` and `$value`? Can you show a bit more of the code to provide some context for those? Are you iterating the items array of the invoice object shown in the first code block? – Don't Panic Nov 13 '17 at 16:20
  • Please see edit. I have updated to show these things. Sorry for neglecting this at first. – heidi sievert Nov 13 '17 at 16:30
  • I don't see how this can be relating to how i quote strings. As when i pull this in from the API, and try cast it as an array from an object, and print_r it, it still has the exact same format shown above? @tyteen4a03 – heidi sievert Nov 13 '17 at 16:36
  • `print_r()` will introspect the object in its own way, so it has nothing to do with converting the object to a string. Are you absolutely sure both `$key` and `$value` are strings? What's the output of `gettype()` on these variables? – tyteen4a03 Nov 13 '17 at 16:54
  • @tyteen4a03 key is a string, however value appears to be an object. Which is odd since that is coming from $data which is an array(I tested this with gettype() in the function), so somehow it's choosing to convert an array to an object. – heidi sievert Nov 13 '17 at 17:37

0 Answers0