我使用this库来读取/写入CSV文件:
$csv = new SimpleExcel('CSV');
$csv->parser->loadFile('test.csv');
$csv->convertTo('JSON');
输出是一个对象:
SimpleExcel\SimpleExcel Object
(
[parser] => SimpleExcel\Parser\CSVParser Object
(
[delimiter:protected] => ,
[file_extension:protected] => csv
[table_arr:protected] => Array
(
[0] => Array
(
[0] => Title
[1] => COST PRICE
[2] => REGULAR POST
[3] => REGISTERED POST
[4] => EXPRESS POST
)
...
...但我无法将其转换为json_decode
(expects parameter 1 to be string, object given
)的数组,也无法迭代table_arr
,因为它是受保护的数组。
答案 0 :(得分:0)
为此废弃了图书馆:
$csv = array_map('str_getcsv', file('test.csv'));