Reputation: 411
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] => [email protected]
[country] =>
[city] =>
[zip] =>
[fullname] => lori
)
[restaurant] => Array
(
[notes] => Email: [email protected]
[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] => [email protected]
[country] =>
[city] =>
[zip] =>
[fullname] => lori
)
[restaurant] => Array
(
[notes] => Email: [email protected]
[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] => [email protected]
[phone] => 32423432
)
[restaurant] => Array
(
[name] => Restaurant Würe
[address] => Hauptstrasse 43a Bronschhofen
[zip] =>
[city] =>
[country] =>
[notes] => Email: [email protected]
)
)
)
)
Could you please help me to create this foreach loop?
Upvotes: 0
Views: 35
Reputation: 316
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
Upvotes: 1