mercredi 20 janvier 2016

JSON loop gives one array more than expected


I have a strange situation with my PHP code.
Basically I want to output the JSON of all the JSON files I have in a directory, but for an unexpected reason it gives me two JSON arrays, one is wrong and one is correct.
Here's the code:

$files = glob("players/*");
$nFiles = count($files);

foreach($files as $file){

  $jsonArray[] = array(

    "name" => "a name",
    "reason" => "a reason",
    "date" => "a date"

  );

  echo json_encode($jsonArray);
}

Output:

[{"name":"a name","reason":"a reason","date":"a date"}] -- here the loop stops (idk why) and it begins again -- [{"name":"a name","reason":"a reason","date":"a date"},{"name":"a name","reason":"a reason","date":"a date"}]




Aucun commentaire:

Enregistrer un commentaire