Here's my problem. I have a JSON file like this:
[
{
"projectName": "test",
"clientName": "test2",
"dateValid": "2014-04-18",
"account": {
"accountAmount": null,
"accountDate": "2014-04-19",
"accountType": null
},
"total": {
"totalAmount": null,
"totalDate": "2014-04-18",
"totalType": null
}
}]
And I want PHP to open this file and add another object, so my file will look like this :
[
{
"projectName": "test",
"clientName": "test2",
"dateValid": "2014-04-18",
"account": {
"accountAmount": null,
"accountDate": "2014-04-19",
"accountType": null
},
"total": {
"totalAmount": null,
"totalDate": "2014-04-18",
"totalType": null
}
},
{
"projectName": "test",
"clientName": "test2",
"dateValid": "2014-04-18",
"account": {
"accountAmount": null,
"accountDate": "2014-04-19",
"accountType": null
},
"total": {
"totalAmount": null,
"totalDate": "2014-04-18",
"totalType": null
}
}
]
It should be quite simple, but I can't achieve that. I tried multiple ways to do that :
$file = 'base.json';
if(file_exists ($file)){
echo 'base.json found';
$fileContent = file_get_contents($file);
$oldData = json_decode($fileContent, true);
echo var_export($oldData);
}
else {
echo 'base.json not found';
$oldData = [];
}
echo $data;
$data = json_encode($data);
$oldData = json_encode($oldData);
echo $data; // debug
file_put_contents('base.json', '['.$data.','.$oldData.']');
Yeah, I putted lots of echo to debug the data process... What do I am missing ?