2

我有一个由用户在 Microsoft Excel 中生成的 .CSV 文件,需要将其转换为 Web 表单。

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
4

2 回答 2

1

我可以回答你问题的最后一部分。让 json 输出到多行是通过将JSON_PRETTY_PRINT常量传递给json_encode. 如果您使用的是 php 5.4+,则此功能可用。

echo json_encode($partialCase, JSON_PRETTY_PRINT);
于 2013-10-17T00:34:21.683 回答
1

经过大量的修补,我设法解决了这个问题。这一行:

'contents' => $data[4],

需要转义符和逗号,如下所示:

'contents' => htmlentities($data[4], ENT_NOQUOTES)

并且因为"’"没有转义字符,所以需要"'"在 CSV 文件中更改为。

于 2013-10-17T10:44:22.813 回答