douwei2966 2018-04-12 04:21
浏览 39

从xml节点提取值并在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. 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条回答 默认 最新

  • duanpasi6287 2018-04-12 04:30
    关注

    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])
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程