jeudi 30 novembre 2017

PHP: Parsing JSON with foreach()

I tried decoding a JSON file and display its contents. My PHP code for this is:

<?php        
  $proj = json_decode(file_get_contents('projects.json'));
  foreach ($proj->projects as $p) {
    $name = $p->name;
    $auth = $p->author;
    $file = $p->filep;
    project($name, $auth, $file);
  }
?>

and my JSON file is:

{
    "projs": [
        {
            "name": "RGBFade",
            "author": "me",
            "filep": "rgbfade.txt"
        },
    ]
}

The project() function and the implementation of the JSON File works.

However, it throws this error:

Warning: Invalid argument supplied for foreach() in index.php on line 38

Does anyone know how to fix this?

If you would like to see the site, it's here




Aucun commentaire:

Enregistrer un commentaire