php读取csv文件返回json,使用PHP将CSV转换为JSON

我正在尝试使用PHP将CSV文件转换为JSON。

这是我的代码

date_default_timezone_set('UTC');

$today = date("n_j"); // Today is 1/23/2015 -> $today = 1_23

$file_name = $today.'.CSV'; // My file name is 1_23.csv

$file_path = 'C:\\Users\\bheng\\Desktop\\qb\\'.$file_name;

$file_handle = fopen($file_path, "r");

$result = array();

if ($file_handle !== FALSE) {

$column_headers = fgetcsv($file_handle);

foreach($column_headers as $header) {

$result[$header] = array();

}

while (($data = fgetcsv($file_handle)) !== FALSE) {

$i = 0;

foreach($result as &$column) {

$column[] = $data[$i++];

}

}

fclose($file_handle);

}

// print_r($result); // I see all data(s) except the header

$json = json_encode($result);

echo $json;

?>

print_r($result); //我看到了所有数据

然后我json_encode($result);尝试显示它,但是屏幕上什么都没有显示。我所看到的只是空白屏幕和0错误消息。

我做错什么了吗?有人能帮我吗 ?

的相加结果 print_r($result);

Array (

[Inventory] => Array (

[0] => bs-0468R(20ug)

[1] => bs-1338R(1ml)

[2] => bs-1557G(no bsa)

[3] => bs-3295R(no BSA)

[4] => bs-0730R-Cy5"

[5] => bs-3889R-PE-Cy7"

[6] => 11033R

[7] => 1554R-A647

[8] => 4667

[9] => ABIN731018

[10] => Anti-DBNL protein

.... more ....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值