我有一个由Microsoft Excel中的用户生成的.CSV文件,需要将其转换为网络表单。
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
我设法成功将其导入PHP并使用它来正确生成表单,但现在它不能正确地传入JSON。
<?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);
?>
最初它是从CSV中作为单个数组获取,然后被分成两个数组,但我发现这是不必要的并且把它拿出来[它也没有工作]。我想撕掉头发,想知道为什么一个阵列正确通过,而另一个阵列没有。
下面是输出,我也无法弄清楚它为什么不分成多行。编辑:由于@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
答案 0 :(得分:1)
我可以回答你问题的最后部分。通过将JSON_PRETTY_PRINT
常量传递给json_encode
来完成将json输出到多行。如果您使用的是PHP 5.4+,则可以使用此功能。
echo json_encode($partialCase, JSON_PRETTY_PRINT);
答案 1 :(得分:1)
经过大量的修补后,我设法解决了这个问题。 这一行:
'contents' => $data[4],
需要&符号和逗号进行转义,如下所示:
'contents' => htmlentities($data[4], ENT_NOQUOTES)
由于"’"
没有转义字符,因此需要在CSV文件中将其更改为"'"
。