这是我的制表符分隔的CSV(顺便说一下,我在这里制作了空格而不是标签):
header1 header2 header3 header4
value1 value2 value3
value2 value2
如果我使用此功能,它会将CSV作为数组返回但不完整:
$content = file($file, FILE_SKIP_EMPTY_LINES);
$rows = array_map('str_getcsv', $content, array_fill(0, count($content), "\t"));
这不起作用:
if(file_exists($file)){
$rows = array_map(function($v){
return str_getcsv($v, "\t");
}, file($file, FILE_SKIP_EMPTY_LINES));
}
输出:
Array(
[0] => Array(
[0] => header1
[1] => header2
[2] => header3
[3] => header4
)
[1] => Array(
[0] => value1
[1] => value2
[2] => value3
)
[2] => Array(
[0] => value1
[1] => value2
)
)
似乎它将空标签视为一个,因此值与标题相比将具有不完整的数组。
预期:
Array(
[0] => Array(
[0] => header1
[1] => header2
[2] => header3
[3] => header4
)
[1] => Array(
[0] => value1
[1] => value2
[2] => value3
[3] =>
)
[2] => Array(
[0] => value1
[1] => value2
[2] =>
[3] =>
)
)
答案 0 :(得分:0)
如果你想输出如:
阵列( [0] =>头1 [1] => HEADER2 [2] => header3 [3] => header4 [4] =>值1 [5] =>值2 ... )
只需更改您的数据csv,但如果您不想更改它。
你可以尝试使用explode功能