循环多维数组

时间:2017-03-21 14:48:26

标签: php arrays multidimensional-array

我有一个多维数组,我希望通过foreach循环访问和循环。我需要支持访问以下项目:

-deliveryMethod

-totalPrice

-preparationTime

- [送货] [电子邮件]

- [notes] =>电子邮件:

 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循环吗?

1 个答案:

答案 0 :(得分: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替换为数组的名称