1

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

I also added the foreach example in the answer ;)
I appreciate the help! Array's do make it easier to work with. I am working on localhost and had to add ['Message'] to make it foreach($xmlToArray['Message']['SettlementReport']['Refund'] as $ful) otherwise I got an Undefined index: SettlementReport -- but it works well! Thank you again.

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.