Foreach XML如果仅循环返回第一个节点

时间:2016-11-11 00:12:24

标签: php xml

我正在尝试获取值int[] array1 = new Random().ints(10, 20, 51).toArray(); int[] temp = Arrays.stream(array1).filter(x -> x > 35).toArray(); int[] array2 = new int[10]; System.arraycopy( temp, 0, array2, 0, temp.length ); System.out.println(Arrays.toString(array1)); System.out.println(Arrays.toString(array2)); 的每个SKU的{​​{1}}个。

这是我的xml:

MerchantFulfillmentID

我的PHP

MFN

我的结果是

  

MFN

     

1MFNSKU1

仅从它找到的第一次迭代中获取结果。如何才能使$xml = '<?xml version="1.0" encoding="UTF-8"?> <Message> <SettlementReport> <Refund> <Fulfillment> <MerchantFulfillmentID>MFN</MerchantFulfillmentID> <AdjustedItem> <SKU>1MFNSKU1</SKU> </AdjustedItem> </Fulfillment> </Refund> <Refund> <Fulfillment> <MerchantFulfillmentID>AFN</MerchantFulfillmentID> <AdjustedItem> <SKU>1AFNSKU1</SKU> </AdjustedItem> </Fulfillment> </Refund> <Refund> <Fulfillment> <MerchantFulfillmentID>MFN</MerchantFulfillmentID> <AdjustedItem> <SKU>2MFNSKU2</SKU> </AdjustedItem> </Fulfillment> </Refund> <Refund> <Fulfillment> <MerchantFulfillmentID>AFN</MerchantFulfillmentID> <AdjustedItem> <SKU>2AFNSKU2</SKU> </AdjustedItem> </Fulfillment> </Refund> </SettlementReport> </Message>'; 的每个实例都回显$xmlload = simplexml_load_string($xml); foreach ($xmlload->Message->SettlementReport->Refund->Fulfillment as $ful) { if ($ful->MerchantFulfillmentID == 'MFN') { echo $ful->MerchantFulfillmentID; echo '<br />'; echo $ful->AdjustedItem->SKU; } }

1 个答案:

答案 0 :(得分:1)

我个人会根据你的XML创建一个数组。

$xmlToString   = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$xmlToArray = json_decode(json_encode((array)$xmlToString), TRUE);

print_r($xmlToArray);

这将打印出来;

Array
(
    [SettlementReport] => Array
        (
            [Refund] => Array
                (
                    [0] => Array
                        (
                            [Fulfillment] => Array
                                (
                                    [MerchantFulfillmentID] => MFN
                                    [AdjustedItem] => Array
                                        (
                                            [SKU] => 1MFNSKU1
                                        )

                                )

                        )

                    [1] => Array
                        (
                            [Fulfillment] => Array
                                (
                                    [MerchantFulfillmentID] => AFN
                                    [AdjustedItem] => Array
                                        (
                                            [SKU] => 1AFNSKU1
                                        )

                                )

                        )

                    [2] => Array
                        (
                            [Fulfillment] => Array
                                (
                                    [MerchantFulfillmentID] => MFN
                                    [AdjustedItem] => Array
                                        (
                                            [SKU] => 2MFNSKU2
                                        )

                                )

                        )

                    [3] => Array
                        (
                            [Fulfillment] => Array
                                (
                                    [MerchantFulfillmentID] => AFN
                                    [AdjustedItem] => Array
                                        (
                                            [SKU] => 2AFNSKU2
                                        )

                                )

                        )

                )

        )

)

然后在&#34; $ xmlToArray&#34;上做一个foreach。


    foreach($xmlToArray['SettlementReport']['Refund'] as $ful)
    {
        if( $ful['Fulfillment']['MerchantFulfillmentID'] == 'MFN')
        {
            echo $ful['Fulfillment']['MerchantFulfillmentID'];
            echo $ful['Fulfillment']['AdjustedItem']['SKU'];

        }
    }

相关艺术:Converting a SimpleXML Object to an Array