PHP嵌套数组循环嵌套结果

时间:2014-05-28 13:49:23

标签: php arrays loops

我正在尝试使用我的第一个API并坚持使用结果。我正在获得一个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
                                        )

                                )

                        )

                )

        )

)

现在我知道如何在数组中引用fied(如命令id)的onyl方式:

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

但是我希望能够遍历订单数组并为每个项目执行代码,有人可以指出我正确的方向:

a)有没有更好的方法来推荐这些领域? b)如何循环遍历数组的OrderResponseItem部分?

我能想到的唯一循环是整个数组不是数组中的嵌套项。

对不起,如果我错过了一些简单的事情......

谢谢,如果您需要任何其他格式的数据,请告诉我。

1 个答案:

答案 0 :(得分:3)

由于你已经知道了密钥,你可以使用foreach访问它们,然后循环指向该密钥。像这样:

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