drvxnivoqf17568697 2017-03-21 14:48
浏览 23
已采纳

循环多维数组

I have a multidimensional array and I want access and loop through with a foreach loop. I need to support to access to following items:

-deliveryMethod

-totalPrice

-preparationTime

-[shipping][email]

-[notes] => Email:

 Array
(
    [page] => 1
    [page_size] => 10
    [total] => 3
    [num_pages] => 1
    [result] => Array
        (
            [0] => Array
                (
                    [_id] => 58c9954070f15c0011535f4d
                    [friendlyID] => 460
                    [date] => 1489605952453
                    [status] => STATUS_DELIVERED
                    [deliveryMethod] => METHOD_TAKEAWAY
                    [notes] => 
                    [grandTotal] => 75.56
                    [application] => 58be7dc51fa4bc00111b9a64
                    [__v] => 0
                    [items] => Array
                        (
                            [0] => Array
                                (
                                    [_id] => 58c9954070f15c0011535f4e
                                    [totalPrice] => 75.56
                                    [preparationTime] => 10
                                    [unitPrice] => 18.89
                                    [quantity] => 4
                                    [name] => Calabrese 29cm
                                    [tax] => Array
                                        (
                                            [value] => 23
                                            [percentage] => 0.23
                                        )

                                )

                        )

                    [shipping] => Array
                        (
                            [phone] => 32423432
                            [email] => info@lovanet.ch
                            [country] => 
                            [city] => 
                            [zip] => 
                            [fullname] => lori
                        )

                    [restaurant] => Array
                        (
                            [notes] => Email: info@lovanet.ch
                            [country] => 
                            [city] => 
                            [zip] => 
                            [address] => Hauptstrasse 43a Bronschhofen
                            [name] => Restaurant Würe
                        )

                )

            [1] => Array
                (
                    [_id] => 58c9948170f15c0011535f4a
                    [friendlyID] => 459
                    [date] => 1489605761859
                    [status] => STATUS_SHIPPED
                    [deliveryMethod] => METHOD_TAKEAWAY
                    [notes] => 
                    [grandTotal] => 54.68
                    [application] => 58be7dc51fa4bc00111b9a64
                    [__v] => 0
                    [items] => Array
                        (
                            [0] => Array
                                (
                                    [_id] => 58c9948170f15c0011535f4c
                                    [totalPrice] => 12.9
                                    [preparationTime] => 10
                                    [unitPrice] => 12.9
                                    [quantity] => 1
                                    [name] => Calabrese 22cm
                                    [tax] => Array
                                        (
                                            [value] => 23
                                            [percentage] => 0.23
                                        )

                                )

                            [1] => Array
                                (
                                    [_id] => 58c9948170f15c0011535f4b
                                    [totalPrice] => 41.78
                                    [preparationTime] => 10
                                    [description] => Extra Salami- $2.00

                                    [unitPrice] => 18.89
                                    [quantity] => 2
                                    [name] => Calabrese 29cm
                                    [tax] => Array
                                        (
                                            [value] => 23
                                            [percentage] => 0.23
                                        )

                                )

                        )

                    [shipping] => Array
                        (
                            [phone] => 32423432
                            [email] => info@lovanet.ch
                            [country] => 
                            [city] => 
                            [zip] => 
                            [fullname] => lori
                        )

                    [restaurant] => Array
                        (
                            [notes] => Email: info@lovanet.ch
                            [country] => 
                            [city] => 
                            [zip] => 
                            [address] => Hauptstrasse 43a Bronschhofen
                            [name] => Restaurant Würe
                        )

                )

            [2] => Array
                (
                    [_id] => 58be912e1fa4bc00111b9ae6
                    [friendlyID] => 443
                    [date] => 1488884014509
                    [status] => STATUS_IN_PROGRESS
                    [deliveryMethod] => METHOD_TAKEAWAY
                    [notes] => 
                    [grandTotal] => 12.9
                    [application] => 58be7dc51fa4bc00111b9a64
                    [__v] => 0
                    [items] => Array
                        (
                            [0] => Array
                                (
                                    [name] => Calabrese 22cm
                                    [quantity] => 1
                                    [unitPrice] => 12.9
                                    [preparationTime] => 10
                                    [totalPrice] => 12.9
                                    [_id] => 58be912e1fa4bc00111b9ae7
                                    [tax] => Array
                                        (
                                            [percentage] => 0.23
                                            [value] => 23
                                        )

                                )

                        )

                    [shipping] => Array
                        (
                            [fullname] => lori
                            [zip] => 
                            [city] => 
                            [country] => 
                            [email] => info@lovanet.ch
                            [phone] => 32423432
                        )

                    [restaurant] => Array
                        (
                            [name] => Restaurant Würe
                            [address] => Hauptstrasse 43a Bronschhofen
                            [zip] => 
                            [city] => 
                            [country] => 
                            [notes] => Email: info@lovanet.ch
                        )

                )

        )

)

Could you please help me to create this foreach loop?

  • 写回答

1条回答 默认 最新

  • duanlaofu4108 2017-03-21 15:26
    关注
    foreach ($myArrayName['result'] as $entry) {
        foreach ($myArrayName['items'] as $item) {
            echo 'totalPrice: ' . $item['totalPrice'] . '<br />';
            echo 'preparationTime: ' . $item['preparationTime'] . '<br />';
        }
        echo 'deliveryMethod:' . $entry['deliveryMethod'] . '<br />';
        echo 'E-Mail: ' . $entry['shipping']['email'] . '<br />';
        echo 'E-Mail 2: ' . substr($entry['restaurant']['notes'], 7) . '<br />';
    }
    

    try this code and replace $myArrayName with the name of your array

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 如何将下列的“无限压缩存储器”设计出来
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口