This one may be a tad tricky to understand if I'm not clear. I want to open a .json file, which contains an array and add/append to that array but I keep coming into format problems with the array. I first thought this needed to be PHP but I don't see why it couldn't be JS now.
Some may ask, why aren't I using a database, it just won't work for what I want to do. I just what to store data in a file on-site without a database.
analytics.php on execution adds the data to the data.json file. The plan is to call this on every visit (small site).
$visitArray = [];
$visitArray["ip"] = "127.0.0.1";
$visitArray["referrer"] = "www.google.com";
$visitArray["conversion"] = "nil";
$visitArray["bounce"] = "nil";
$visitArray["platform"] = "Chrome";
$VisiterArray[] = $visitArray;
$data = json_encode($VisiterArray);
$data = $data;
file_put_contents("data.json", $data, FILE_APPEND);
data.json (when analytics.php is executed)
[
{
"ip":"127.0.0.1",
"referrer":"www.google.com",
"conversion":"nil",
"bounce":"nil",
"platform":"Chrome"
}
]
The problem is if you run the analytics.php there's no "," and I don't think it's as easy to solve to change $data = $data; to $data = $data.','; it just seems like a terrible way to do things.
[
{
"ip":"127.0.0.1",
"referrer":"www.google.com",
"conversion":"nil",
"bounce":"nil",
"platform":"Chrome"
}
]
// missing ","
[
{
"ip":"127.0.0.1",
"referrer":"www.google.com",
"conversion":"nil",
"bounce":"nil",
"platform":"Chrome"
}
]