简体   繁体   English

从xml节点提取值并添加到php中的数组

[英]Extract values from xml node and add to array in php

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. 我一直在寻找针对此特定问题的答案,但没有找到答案-如果我忽略了某些内容,我道歉...这是我的问题...我正在建立两个数据库之间的集成,并且正在使用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. 我从源API获得XML响应,然后需要从该响应中提取特定的数据值,并将它们添加到数组中,以通过其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: 这是源代码中的xml格式的快照:

<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: 我使用以下方法将xml转换为php中的数组:

$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! 谢谢!

Its look like there is array in $sodoc->DOCNO and $sodoc->WHENPOSTED So grab the first index while you getting data 它看起来像在$sodoc->DOCNO$sodoc->WHENPOSTED存在数组,因此在获取数据时获取第一个索引

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM