dswg47377 2014-05-28 13:49
浏览 56
已采纳

PHP嵌套数组循环嵌套结果

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.

  • 写回答

1条回答 默认 最新

  • douyanjing0822 2014-05-28 14:00
    关注

    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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题