-3

I have a json file and i need a loop for echo the contnent for each id value

like this:

{
"dati": [
   {
    "id": 96984,
    "sottotitolo": "test sottotitolo",
    "img": "https://sdsds.com",
    "url": "asdsa.com",
    "stato": "IT",
    "regione": "IT.08",
   }

   { 
    "id": 24543,
    "sottotitolo": "test sottotitolo2",
    "img": "https://sdsds.com",
    "url": "asdsa.com",
    "stato": "IT",
    "regione": "IT.08",
  }
]
}

to something like this:

<div id"96984"> <h3>test sottotitolo</h3> ......... </div> <div id"24543"> <h3>test sottotitolo2</h3> ......... </div>
Phiter
  • 14,570
  • 14
  • 50
  • 84
nrik
  • 23
  • 8

1 Answers1

0

You can do something like this:

$data = file_get_contents("/path/to/your/file.json");
$dataArray = json_decode($data, true);

foreach ($dataArray as $row){
    foreach ($row as $key => $value){
        switch ($key) {
            case 'id':
              echo  "<div> $value </div>";
            break;
            case 'sottotitolo':
              echo  "<h3> $value </h3>";
            break;
            case 'img':
              echo  "<img src=$value >";
            break;
           // ...
        }

    }
}
Sasindu Mendis
  • 346
  • 2
  • 7