bbruman bbruman - 1 month ago 11
PHP Question

Foreach XML If Loop Only Returning First Nodes

I am trying to get the

SKU
's of each
MerchantFulfillmentID
that has the value
MFN
in it.

Here is my xml:

$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>';


And my PHP

$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;
}
}


My result is


MFN

1MFNSKU1


Which is only taking the result from the first iteration it finds. How can I make it so that every instance of
MFN
will echo out the
SKU
?

Answer

Personally i would create an array from your XML as below.

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

print_r($xmlToArray);

This will print out;

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
                                        )

                                )

                        )

                )

        )

)

Then do a foreach on "$xmlToArray".


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

        }
    }

Related artile: Converting a SimpleXML Object to an Array