简体   繁体   中英

foreach loop for multidimensional array in php

I have a multidimensional array with key value. I want to loop the data in that array but I don't know how.

This is my array:

{
  "success": "1",
  "order_details": [
    {
      "item_order": 5,
      "address": "155, Mani Nagar Society, Nana Varachha, Surat, Gujarat 395006, India",
      "contact": "95303709",
      "total_price": "3330.0",
      "order_place": "18-05-25 06-07-20",
      "preparing_date_time": "",
      "preparing_status": "Deactivate",
      "dispatched_date_time": "",
      "dispatched_status": "Deactivate",
      "delivered_date_time": "",
      "delivered_status": "Deactivate",
      "menu": [
        {
          "menu_name": "demo item",
          "item_amt": "200",
          "Ingredients": [
            {
              "ingredients_name": "burger",
              "ingredients_price": "200"
            },
            {
              "ingredients_name": "pizza1",
              "ingredients_price": "800"
            }
          ]
        }
      ]
    }
  ]
}

How do I loop / foreach that array?

I guess there is a foreach inside a foreach, but I don't know how to do that.

In the manual of array_walk_recursive there is an example that might suit you.
http://php.net/manual/en/function.array-walk-recursive.php

$arr = json_decode($str, true); //$str is your json string

array_walk_recursive($arr, 'test_print');

function test_print($item, $key)
{
    echo "[$key]: $item\n";
}

https://3v4l.org/dVUMS

If you only want to output some elements of the array you can create an array with the items you want to output and pass it on the function.
Then use in_array to see if it's a output or not.

https://3v4l.org/8ZvUS

Use this code:

    <?php

$jsonData = '{
  "success": "1",
  "order_details": [
    {
      "item_order": 5,
      "address": "155, Mani Nagar Society, Nana Varachha, Surat, Gujarat 395006, India",
      "contact": "95303709",
      "total_price": "3330.0",
      "order_place": "18-05-25 06-07-20",
      "preparing_date_time": "",
      "preparing_status": "Deactivate",
      "dispatched_date_time": "",
      "dispatched_status": "Deactivate",
      "delivered_date_time": "",
      "delivered_status": "Deactivate",
      "menu": [
        {
          "menu_name": "demo item",
          "item_amt": "200",
          "Ingredients": [
            {
              "ingredients_name": "burger",
              "ingredients_price": "200"
            },
            {
              "ingredients_name": "pizza1",
              "ingredients_price": "800"
            }
          ]
        }
      ]
    }
  ]
}';

$jsonDecode = json_decode($jsonData);



foreach ($jsonDecode->order_details as $orderDetail) {

    echo "Item order: " . $orderDetail->item_order;
    echo "<br>";
    echo "Address: " . $orderDetail->address;
    echo "<br>";
    echo "Contact: " . $orderDetail->contact;
    echo "<br>";

    foreach ($orderDetail->menu as $menuItem) {

        echo "Menu Name: " . $menuItem->menu_name;
        echo "<br>";
        echo "Item amt: " . $menuItem->item_amt;
        echo "<br>";

        foreach ($menuItem->Ingredients as $ingredientsItem) {

            echo "Ingredients name: " . $ingredientsItem->ingredients_name;
            echo "<br>";
            echo "Ingredients price: " . $ingredientsItem->ingredients_price;
            echo "<br>";
        }
    }
}

Output:

Item order: 5
Address: 155, Mani Nagar Society, Nana Varachha, Surat, Gujarat 395006, India
Contact: 95303709
Menu Name: demo item
Item amt: 200
Ingredient name: burger
Ingredient price: 200
Ingredient name: pizza1
Ingredient price: 800

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM