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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?