Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am experimenting with my first API and getting stuck with the results. I am getting an Array Back:

Array
(
    [GetOrderListResult] => Array
        (
            [Status] => Success
            [MessageCode] => 0
            [ResultData] => Array
                (
                    [OrderResponseItem] => Array
                        (
                            [0] => Array
                                (
                                    [NumberOfMatches] => 2
                                    [OrderTimeGMT] => 2014-05-05T03:23:00
                                    [LastUpdateDate] => 2014-05-28T11:41:45.953
                                    [TotalOrderAmount] => 12.7800
                                    [OrderState] => Active
                                    [DateCancelledGMT] => 
                                    [OrderID] => 138711
                                    [ClientOrderIdentifier] => 138711
                                    [SellerOrderID] => 
                                    [OrderStatus] => Array
                                        (
                                            [CheckoutStatus] => NotVisited
                                            [CheckoutDateGMT] => 1900-01-01T00:00:00
                                            [PaymentStatus] => NotSubmitted
                                            [PaymentDateGMT] => 1900-01-01T00:00:00
                                            [ShippingStatus] => Unshipped
                                            [ShippingDateGMT] => 1900-01-01T00:00:00
                                            [OrderRefundStatus] => NoRefunds
                                        )

                                )

                            [1] => Array
                                (
                                    [NumberOfMatches] => 2
                                    [OrderTimeGMT] => 2014-05-05T03:23:00
                                    [LastUpdateDate] => 2014-05-28T12:59:01.78
                                    [TotalOrderAmount] => 6.3900
                                    [OrderState] => Active
                                    [DateCancelledGMT] => 
                                    [OrderID] => 138750
                                    [ClientOrderIdentifier] => 138750
                                    [SellerOrderID] => 
                                    [OrderStatus] => Array
                                        (
                                            [CheckoutStatus] => NotVisited
                                            [CheckoutDateGMT] => 1900-01-01T00:00:00
                                            [PaymentStatus] => NotSubmitted
                                            [PaymentDateGMT] => 1900-01-01T00:00:00
                                            [ShippingStatus] => Unshipped
                                            [ShippingDateGMT] => 1900-01-01T00:00:00
                                            [OrderRefundStatus] => NoRefunds
                                        )

                                )

                        )

                )

        )

)

Now the onyl way I know how to reference a fied such as the order id in the array is:

  echo "Order ID: ".$result['GetOrderListResult']['ResultData']['OrderResponseItem']['0']['OrderID'];

But I want to be able to loop through the array of orders and execute code for each item, could somewbody please point me in the right direction for:

a) is there a better way to refernce these fields? b) how do I loop through the OrderResponseItem part of the array?

The only loop I could think of was for the whole array not nested items in the array.

Sorry if I'm missing something simple....

Thanks and if you need the data in any other format please let me know.

share|improve this question
2  
a) b) foreach($result['GetOrderListResult']['ResultData']['OrderResponseItem'] as $order) –  Peter May 28 at 13:51
1  
Thanks @Peter Perfect, didn't realise I could reference the array inside a loop like that. New to PHP. Thanks again though much appreciated. –  Paul May 28 at 14:01

1 Answer 1

up vote 3 down vote accepted

Since you already know the keys you could just use a foreach to access them an point to that key then loop. Something like this:

foreach($result['GetOrderListResult']['ResultData']['OrderResponseItem'] as $value) {
    $order_id = $value['OrderID'];
    // your other processes
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.