I have a .CSV file, generated by the user in Microsoft Excel, that needs to be turned into a web form.
id,partial-allowed,amount,description,contents,price
0,TRUE,88 count,California Fancy Navels,,27
1,TRUE,48 count,Texas Rio Star Red Grapefruit,,22
2,TRUE,100 count,Washington Red Delicious Apples,,40
3,TRUE,100 count,Washington Golden Delicious Apples,,40
4,TRUE,100 count,Braeburns,,40
5,FALSE,40 count,Apple Sampler,"8 Red Delicious, 8 Golden Delicious, 8 Granny Smith, 8 Fuji, 8 Braeburn",22
6,FALSE,20 count,Apple Sampler,"4 Red Delicious, 4 Golden Delicious, 4 Granny Smith, 4 Fuji, 4 Braeburn",16
7,FALSE,18 pound,Number 1 Mixed Fruit Box,"18 Navel Oranges, 8 Red Delicious Apples, 10 Braeburn Apples",20
8,FALSE,18 pound,Number 2 Mixed Fruit Box,"14 Navel Oranges, 6 Red Delicious & 6 Braeburns Apples, 10 D’Anjou Pears",20
9,FALSE,18 pound,Number 3 Mixed Fruit Box,"8 Grapefruit, 12 Navel Oranges, 10 Braeburn Apples",20
10,FALSE,18 pound,Number 4 Mixed Fruit Box,"6 Grapefruit, 12 Navel Oranges, 4 Red Delicious & 4 Braeburn Apples 6 D’Anjou Pears",20
11,FALSE,18 pound,Number 5 Mixed Fruit Box,11 Grapefruit and 18 Oranges,20
I managed to successfully import this into PHP and use it to generate the form properly, but now it won't pass into JSON properly.
<?php
$csvHandle = fopen('Sale Data/Fruit-Sale.2013.csv', 'r');
$partialCase = array();
$completeCase = array();
//used to skip the header row
$keys = fgetcsv($csvHandle, 0, ',');
while(($data = fgetcsv($csvHandle, 0, ',')) !== FALSE) {
$item = array(
//changes the string value to a numeric value
'id' => intval($data[0]),
//changes the string value to a boolean value
'partial-allowed' => filter_Var($data[1], FILTER_VALIDATE_BOOLEAN),
'amount' => $data[2],
'description' => $data[3],
'contents' => $data[4],
//changes the string value to a numeric value
'price' => intval($data[5])
);
$item['partial-allowed'] == true ?
array_push($partialCase, $item) : array_push($completeCase, $item);
}
echo "\npartialCase\n";
//outputs properly as an array, or an object if I pass the constant JSON_FORCE_OBJECT
echo json_encode($partialCase);
echo "\ncompleteCase\n";
//outputs nothing
echo json_encode($completeCase);
?>
Originally it was taken from the CSV as a single array and then was split into the two arrays, but I found that was unnecessary and took it out [it didn't work then either]. I'm at the point of tearing out hair wondering why one array is passed properly while the other is not.
Below is the output, I also couldn't figure out why it wasn't splitting into multiple lines. EDIT: This was corrected, thanks to @Duotrigesimal
partialCase[{"id":0,"partial-allowed":true,"amount":"88 count","description":"California Fancy Navels","contents":"","price":27},{"id":1,"partial-allowed":true,"amount":"48 count","description":"Texas Rio Star Red Grapefruit","contents":"","price":22},{"id":2,"partial-allowed":true,"amount":"100 count","description":"Washington Red Delicious Apples","contents":"","price":40},{"id":3,"partial-allowed":true,"amount":"100 count","description":"Washington Golden Delicious Apples","contents":"","price":40},{"id":4,"partial-allowed":true,"amount":"100 count","description":"Braeburns","contents":"","price":40}]completeCase
fgetcsv? Simply tryfgetcsv($csvHandle)