A
Anonymous
Guest
My code so far:
My json file looks like this:
Expected behaviour is to increment the counter value of matching source supplied by POST by 1.
The error in logs is PHP Warning: Invalid argument supplied for foreach() in /file.php on line 9 .
When I do
I get output
Where do I go wrong ?
Thank you.
Code:
<?PHP
header('Access-Control-Allow-Origin: *');
$src = $_POST["src"];
$jsonString = file_get_contents('jsonFile.json');
$data = json_decode($jsonString, true);
foreach ($data as $key => $entry) {
if ($entry['src'] == $src) {
$data[$key]['counter']++;
}
}
$newJsonString = json_encode($data);
file_put_contents('jsonFile.json', $newJsonString);
?>
My json file looks like this:
Code:
[
{
"src": "",
"counter": "0"
},
{
"src": "",
"counter": "0"
} // and so on...
The error in logs is PHP Warning: Invalid argument supplied for foreach() in /file.php on line 9 .
When I do
Code:
var_dump($data)
Code:
NULL
Where do I go wrong ?
Thank you.