This is a little more specific than the other answers I've found or could come up with on my own.
My json file contains exactly
[ "box", "tube" ]
Nothing more and nothing less. With the json data formatted like this, I can get the exact results needed.
What I would like to do is be able to append more to the json file using PHP. I have seen:
<?php
session_start();
if(isset($_POST['add'])){
$data = file_get_contents('members.json');
$data_array = json_decode($data);
//data in our POST
$input = array(
'id' => $_POST['id'],
'firstname' => $_POST['firstname'],
'lastname' => $_POST['lastname'],
'address' => $_POST['address'],
'gender' => $_POST['gender']
);
//append the POST data
$data_array[] = $input;
//return to json and put contents to our file
$data_array = json_encode($data_array, JSON_PRETTY_PRINT);
file_put_contents('members.json', $data_array);
$_SESSION['message'] = 'Data successfully appended';
}
else{
$_SESSION['message'] = 'Fill up add form first';
}
header('location:index.php');
?>
My issue with this is that I don't understand how to use this example in my case since my json data is not using a key for its single array. Do I just have to give up and find a way to use named keys to get the results I want, or is there a way to keep my json data formatted the way I want and also be able to append more data to it?