tyronza
tyronza

Reputation: 1

PHP file_get_contents Multidimensional

By inserting this on top of index.php

<?php
    $items = json_decode(file_get_contents('items.json'));
 ?>

im accessing items.json and decoding it.

Now if i loop through it with <?php foreach($items as $item): ?>

<div class="product_item second_row">
  <div class="border" data-item-id="<?php echo $item[0]->id ?>">
    <div class="product-image">
      <img src="img/<?php echo $item[0]->imgName ?>" data-w-id="f149a3d7-f164-b15b-8428-ba536aa3754a" alt="" class="product_image" />
    </div>
    <div class="product_preis">
      <div data-w-id="f149a3d7-f164-b15b-8428-ba536aa3754c" class="product_price_button-2">
        <div class="like-btn"></div>
        <div class="product_price_button_currency-2">CHF</div>
        <div class="product_price_button_price-2">$<?php echo $item[0]->price ?></div>
        <div class="purchaseicon"></div>
      </div>
    </div>
    <div class="description_div">
      <h1 class="product_description_h1"><?php echo $item[0]->name ?></h1>
      <div class="description_text"><?php echo $item[0]->description ?></div>
    </div>

each of the $item[0] should select the first array which is Salat in this case. I know this is multidimensional array and you access it by specifying the first array and so on

What is missing and prevent it to work properly? I get the items displayed but not all of them. I mean i get id:1 displayed but not id:2 and id:3 and so on.

Below is the file items.json

{
    "Salat": [
      {
        "id": 1,
        "name": "Grüner Salat",
        "price": 750,
        "description": "Blattsalat",
        "imgName": "green_salad-removebg-preview.png"
      },
      {
        "id": 2,
        "name": "Gemischter Salat",
        "price": 850,
        "description":"Blattsalat,Mais,Tomaten,Gurken,Oliven und Karotten",
        "imgName": "gemischter_salat-removebg-preview.png"
      },
      {
        "id": 3,
        "name": "Caprese",
        "price": 1000,
        "description":"Büffelmozzarella mit Tomatenscheiben",
        "imgName": "caprese-removebg-preview.png"
      },
      {
        "id": 4,
        "name": "Funghi e Speck",
        "price": 1200,
        "description": "Grüner Salat mit Pilzen und Speck",
        "imgName": "rucola-salat-removebg-preview.png"
      },
      {
        "id": 5,
        "name": "Griechischer Salat",
        "price": 1000,
        "description":"Blattsalat,Mais,Tomaten,Gurken,Oliven,\nKarotten und Feta-Käse",
        "imgName": "poulet-salat-removebg-preview.png"
      }
    ],
      "Sfizio": [
      {
        "id": 6,
        "name": "Carpaccio di Polipo",
        "price": 1500,
        "description":"Tintenfischcarpaccio garniert mit Kapern und Zwiebeln",
        "imgName": "thunfisch-salat-removebg-preview.png"
      },
      {
        "id": 7,
        "name": "Carpaccio di Salmone",
        "price": 1500,
        "description":"Lachs-Carpaccio garniert mit Kapern und Zwiebeln",
        "imgName": "zwiebel-tomate-salat-removebg-preview.png"
      },
      {
        "id": 8,
        "name": "Polpette di Melanzane",
        "price": 900,
        "description":"Frittierte Auberginenbällchen",
        "imgName": "caprese-removebg-preview.png"
      },
      {
        "id": 9,
        "name": "Melanzane gratinate",
        "price": 800,
        "description":"Gratinierte Auberginen",
        "imgName": "caprese-removebg-preview.png"
      },
      {
        "id": 10,
        "name": "Arancini",
        "price": 900,
        "description":"Frittierte Reisbällchen gefüllt mir Ragùsauce,\nErbsen und Mozzarella",
        "imgName": "arancini-removebg.png"
      }
    ],

    "Pasta": [
      {
        "id": 11,
        "name": "Tortelloni al profumo di Tartufo",
        "price": 2550,
        "description":"Trüffel-Parfümierte Tortelloni mit Ricottafüllung",
        "imgName": "arancini-removebg.png"
      },
      {
        "id": 12,
        "name": "Gnocchi Pesto e Mascarpone",
        "price": 2350,
        "description":"Gnocchi gefüllt mit grünem Pesto in\nMascarpone und Pesto Sauce",
        "imgName": "arancini-removebg.png"
      },
      {
        "id": 13,
        "name": "Tagliolini ai Funghi Porcini",
        "price": 2350,
        "description":"Tagliolini in Steinpilz-Rahmsauce",
        "imgName": "paste-tagliatelle-porcini.png"
      },
      {
        "id": 14,
        "name": "Lasagna al Forno",
        "price": 1800,
        "description":"Lasagne mit Käse überbacken",
        "imgName": "paste-lasagna-clasica.png"
      },
      {
        "id": 15,
        "name": "Tagliatelle al Salmone",
        "price": 2350,
        "description":"Tagliatelle mit Lachs in Tomaten-Rahmsauce",
        "imgName": "arancini-removebg.png"
      },
      {
        "id": 16,
        "name": "Melanzane alla Parmiggiana",
        "price": 1800,
        "description":"Feine Auberginenscheiben überbacken\nmit Ragùsauce und Büffelmozzarella",
        "imgName": "arancini-removebg.png"
      },
      {
        "id": 17,
        "name": "Risotto ai Funghi Porcini",
        "price": 1900,
        "description":"Risotto mit Steinpilzen",
        "imgName": "arancini-removebg.png"
      },
      {
        "id": 18,
        "name": "Gnocchi al Gorgonzola",
        "price": 1900,
        "description":"",
        "imgName": "paste-gnocchi-spek-gorgonzola.png"
      }
    ]
  }

Upvotes: 0

Views: 116

Answers (2)

Nathanael
Nathanael

Reputation: 982

it looks like you just need to use 2 loops

<?php foreach($items as $itemGroup): ?>
    <?php foreach($itemGroup as $item): ?>
        <?php echo $item->id ?>
        ...
    <?php endforeach; ?>
<?php endforeach; ?>

Upvotes: 0

Mukul Kumar Jha
Mukul Kumar Jha

Reputation: 1082

Since you are dealing with the associative array you need to get the value of key/index in order to parse it.

In this case, you have to use <?php foreach($items as $key => $item): ?> variant of the foreach loop, where you get the value of index also, now use this $key value to iterate over the array as $item[$key]

Upvotes: 1

Related Questions