【问题标题】:Loop a multidimensional array循环一个多维数组
【发布时间】:2017-03-21 23:26:19
【问题描述】:

我有一个多维数组,我想使用 foreach 循环访问和循环。我需要支持访问以下项目:

-deliveryMethod

-总价格

-准备时间

-[运费][电子邮件]

-[注释] => 电子邮件:

 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
                        )

                )

        )

)

您能帮我创建这个 foreach 循环吗?

【问题讨论】:

  • 你能不能做一个 printf("
    %s
    ", print_r($YOUR_ARRAY, true));并发布屏幕截图或链接,因为很难从本文中理解数组的结构
  • 嘿Bimbonkens。我编辑了我的帖子。是的,它很难读。很抱歉
  • 你尝试了什么?你有没有得到任何错误? Stack Overflow 不是一个让人们为你编写代码的地方。
  • 是的,我知道。我没有发布我的 foreach 循环。这是我开始 foreach ($obj2['result'] as $result) { echo $result['friendlyID'] 的内容。 ' ' 。 $结果['状态'] 。 ' ' 。 $result['deliveryMethod'] 。 '
    '; };

标签: php arrays multidimensional-array


【解决方案1】:
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 />';
}

试试这段代码,用你的数组名替换 $myArrayName

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-05
    • 2015-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多