0

I've searched for an answer to this specific issue and didn't find one - my apologies if I overlooked something... here's my issue... I'm building an integration between two databases, and I'm coding in PHP. I get an XML response from the source API, then need to extract specific data values from that response and add them to an array for insertion into the sink database through its API. I can get to the data values I need from the source, but when I try to add them into the array format the sink requires, I'm getting an array containing the Key/value pair instead of just the value.

Here's a short snippet of the code I'm using to extract the data I need to build the array:

foreach($invoiceResponse->operation->result->data->sodocument as $sodoc){
    $invoice = array("Invoice_ID"=>$sodoc->DOCNO, "Invoice_Date"=>$sodoc->WHENPOSTED)
}

The resulting array looks like this:

{"Invoice_ID":{"0":"SI-000525"},"Invoice_Date":{"0":"03\/22\/2018"}}

And what I want is this:

{"Invoice_ID":"SI-000525","Invoice_Date":"03\/22\/2018"}

Here's a snap of the xml format from the source:

<response>
    <control>
        <status>success</status>
    </control>
    <operation>
        <authentication>
            <status>success</status>
            <sessiontimestamp>2018-04-12T08:14:55-07:00</sessiontimestamp>
        </authentication>
        <result>
            <status>success</status>
            <function>readByQuery</function>
            <data listtype="sodocument" count="1" totalcount="1" numremaining="0" resultId="">
                <sodocument>
                    <PROJECT>GER-0318-DIG-005</PROJECT>
                    <CONTACT.COMPANYNAME>FFF Production Service</CONTACT.COMPANYNAME>
                    <TYPE_OF_INVOICE>Bill in Full</TYPE_OF_INVOICE>
                    <WHENPOSTED>04/10/2018</WHENPOSTED>
                    <STATE>Pending</STATE>
                </sodocument>
...

I convert the xml to an array in php using this:

$response = simplexml_load_string($xml_response);

I can extract the data in a simple statement, for example, a line of code like this:

$query = "DOCHDRID = '".$sodoc->DOCID."'";

results in

DOCHDRNO = 95

so the data is getting extracted correctly here, but when I use this same syntax to add these values to an array I'm experiencing the issue as described above.

I'm sure it's something easy, but I would greatly appreciate any help! Thanks!

1
  • Cast the values to an appropriate type (without casting, they're SimpleXMLElement objects), e.g. $invoice = array("Invoice_ID"=> (int) $sodoc->DOCNO, "Invoice_Date"=> (string) $sodoc->WHENPOSTED); Commented Apr 12, 2018 at 16:01

1 Answer 1

1

Its look like there is array in $sodoc->DOCNO and $sodoc->WHENPOSTED So grab the first index while you getting data

foreach($invoiceResponse->operation->result->data->sodocument as $sodoc){
    $invoice = array("Invoice_ID"=>$sodoc->DOCNO[0], "Invoice_Date"=>$sodoc->WHENPOSTED[0])
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the reply - but that didn't solve the problem. I'm adding more information to the original question to see if that helps...
The combination of the answer and comment solved the problem! the resulting code that worked: $invoice = array("Invoice_ID => (string) $sodoc->DOCNO[0], "Invoice_Date"=>(string) $sodoc->WHENPOSTED[0]) The only ones I'm still having trouble with are data items with node names like CONTACT.COMPANYNAME. when i use the same syntax on those I get a syntax error. Any ideas how to handle these?
ignore second question above - found the answer here: [stackoverflow.com/questions/6531380/… - proper syntax is array("State"=> (string)$sodoc->{'CONTACT.STATE'})

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.