I want to adding array's to an existing .json file with with a HTML form.
this is my PHP:
$myFile = "data.json";
$newArray = array(
'name'=> $_POST['name'],
'date'=> $_POST['date']
);
$fileTmp = file_get_contents($myFile);
$tempArray = json_decode($fileTmp);
array_push($tempArray, $newArray);
$jsonData = json_encode($tempArray);
file_put_contents($myFile, $jsonData);
this is my JSON:
[
{
"name": "name 1",
"date": "01.02.2017"
},
{
"name": "name 2",
"date": "05.02.2017"
},
{
"name": "name 3",
"date": "05.03.2017"
}
]
The problem is i got the warning
"array_push() expects parameter 1 to be array, null given in..."
and in the JSON there's is only null. What is my problem with my code?
$tempArray = json_decode($fileTmp,true); array_push($tempArray, $newArray);try it and tell