1 回答
TA贡献1829条经验 获得超13个赞
我已经修改了你的代码。请尝试一下。它正在工作。
我有删除标题foreach。这不是必需的。此外,在 2ed 循环中删除一对一的数组分配代码并添加$json['data'][] = explode(';', $row[0]);代码。
这是修改后的代码。
<?php
//import csv file
if (!empty($_FILES['csv_file']['name'])) {
$file_data = fopen($_FILES['csv_file']['tmp_name'], 'r');
$csv = fgetcsv($file_data);
//get header column of csv file : 1rst line
$json['columns'] = $csv;
//get rows of csv file
while ($row = fgetcsv($file_data)) {
$json['data'][] = $row;
}
echo json_encode($json);
}
?>
=> 输出
大批:
Array
(
[columns] => Array
(
[0] => Name
[1] => Position
[2] => Office
[3] => Extn.
[4] => Start date
[5] => Salary
)
[data] => Array
(
[0] => Array
(
[0] => Tiger NixonSystem Architect
[1] =>
[2] => Edinburgh
[3] => 5421
[4] => 25/04/2011
[5] => $320800
)
[1] => Array
(
[0] => Garrett Winters
[1] => Accountant
[2] => Tokyo
[3] => 8422
[4] => 25/07/2011
[5] => $170750
)
)
)
JSON:
{"columns":["Name","Position","Office","Extn.","Start date","Salary"],"data":[["Tiger NixonSystem Architect","","Edinburgh","5421","25\/04\/2011","$320800"],["Garrett Winters","Accountant","Tokyo","8422","25\/07\/2011","$170750"]]}
- 1 回答
- 0 关注
- 100 浏览
添加回答
举报