I know there are a lot of answers about saving a php array as a .json file but none of them seem to solve my problem.
Basically I have an array that is storing my data. first I check to see if a .json file has already been created. (each .json file is named using an $id variable)
If there is already a file with that id I want to update it and add more data that I get from my second array $newar
else create the file and add the data from $array.
Everything works apart from the format of the json file. here is the code below.
$array = array(
array(
'First Name' => $scan['FirstName'],
'Last Name' => $scan['LastName'],
'Email' => $scan['Email'],
'Barcode' => $scan['Barcode'],
'Phone' => $scan['Phone'],
'Company' => $scan['Company'],
'Position' => $scan['Position'],
)
);
if (file_exists('client/jsonstore/'.$id.'.json')) {
$json = file_get_contents('client/jsonstore/'.$id.'.json');
$json_data = json_decode($json,true);
$newar = array(
array(
'First Name' => $scan['FirstName'],
'Last Name' => $scan['LastName'],
'Email' => $scan['Email'],
'Barcode' => $scan['Barcode'],
'Phone' => $scan['Phone'],
'Company' => $scan['Company'],
'Position' => $scan['Position'],
)
);
array_push($json_data, $newar);
$json = json_encode($json_data,JSON_PRETTY_PRINT);
file_put_contents('client/jsonstore/'.$id.'.json', $json);
} else {
//Encode the array into a JSON string.
$encodedString = json_encode($array,JSON_PRETTY_PRINT);
//Save the JSON string to a text file.
file_put_contents('client/jsonstore/'.$id.'.json', $encodedString);
//Retrieve the data from our text file.
}
when adding the first set of data my ,json file looks like this :
[
{
"First Name": "first name",
"Last Name": "last name",
"Email": "[email protected]",
"Barcode": "732538896913809762001",
"Phone": "00000000",
"Company": "company",
"Position": "position"
}
]
When adding the next set of data to same file my .json looks like this :
[
{
"First Name": "first name",
"Last Name": "last name",
"Email": "[email protected]",
"Barcode": "732538896913809762001",
"Phone": "00000000",
"Company": "company",
"Position": "position"
},
[
{
"First Name": "first name",
"Last Name": "last name",
"Email": "[email protected]",
"Barcode": "732538896913809762001",
"Phone": "00000000",
"Company": "company",
"Position": "position"
}
]
]
and a var dump looks like this :
0 =>
array (size=7)
'First Name' => string 'first name'
'Last Name' => string 'last name'
'Email' => string '[email protected]'
'Barcode' => string '732538896913809762001'
'Phone' => string '00000000'
'Company' => string 'company'
'Position' => string 'position'
1 =>
array (size=1)
0 =>
array (size=7)
'First Name' => string 'first name'
'Last Name' => string 'last name'
'Email' => string '[email protected]'
'Barcode' => string '732538896913809762001'
'Phone' => string '00000000'
'Company' => string 'company'
'Position' => string 'position'
$arrayand$newarrin your code are arrays of arrays. And probablyarray_push()(a function that is over-used in wrong contexts) doesn't do what you think. Or it does and in this case$newarrmust not be a 2-level array. P.S. If you don't have code that usesarray_pop()then you should not be usingarray_push(). Not because of PHP but because of the logic of your program.