使用PHP的str_getcsv函数将CSV文件数据转换为数组

4

我有一个字符串,它是使用fputcsv函数创建的。

Date,Name,Hours 2013-01-02,"Test User",7:59 2013-01-03,"Test User",7:53 2013-01-04,"Test User",8:12 2013-01-07,"Test User",7:56 2013-01-08,"Test User",8:25 2013-01-09,"Test User",7:56 2013-01-10,"Test User",8:10 2013-01-11,"Test User",7:53 2013-01-14,"Test User",7:54 2013-01-15,"Test User",0:34 2013-04-01,"Test User",5:51 2013-04-02,"Test User",8:50 2013-04-03,"Test User",7:25 2013-04-04,"Test User",8:3 2013-04-05,"Test User","10:42:52[Not punch out]" ,Total,103:1

当设置了适当的头信息(header('Content-type: application/csv'); header('Content-Disposition: attachment; filename="'.$filename.'"');)时,会生成正确的csv文件。
但是我想再将这个字符串转换为数组格式,所以我尝试将上面的字符串传递给str_getcsv函数,但是得到的数组像这样:
Array
(
    [0] => Date
    [1] => Name
    [2] => Hours
2013-01-02
    [3] => "Allen Herrera"
    [4] => 7:59
2013-01-03
    [5] => "Allen Herrera"
    [6] => 7:53
2013-01-04
    [7] => "Allen Herrera"
    [8] => 8:12
2013-01-07
    [9] => "Allen Herrera"
    [10] => 7:56
2013-01-08
    [11] => "Allen Herrera"
    [12] => 8:25
2013-01-09
    [13] => "Allen Herrera"
    [14] => 7:56
2013-01-10
    [15] => "Allen Herrera"
    [16] => 8:10
2013-01-11
    [17] => "Allen Herrera"
    [18] => 7:53
2013-01-14......................

你能帮我得到格式正确的数组吗?这样我就可以轻松地使用它来创建循环表格。


我认为你需要使用PHP_EOL作为分隔符来进行explode(),然后在str_getcsv()中使用每个部分。 - Maxim Khan-Magomedov
5个回答

5
 $file = file_get_contents("test.csv");
 $data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $file));
 print_r($data);

这不会按照我想要的方式给我数组。 - Subodh Ghulaxe

3

经过一番搜索,我找到了解决方案。

$data = array_map("str_getcsv", preg_split('/\r*\n+|\r+/', $string_data));
print_r($data);

给我这样的数组
Array
(
    [0] => Array
        (
            [0] => Date
            [1] => Name
            [2] => Hours
        )

    [1] => Array
        (
            [0] => 2013-01-02
            [1] => Test User
            [2] => 7:59
        )

    [2] => Array
        (
            [0] => 2013-01-03
            [1] => Test User
            [2] => 7:53
        )

    [3] => Array
        (
            [0] => 2013-01-04
            [1] => Test User
            [2] => 8:12
        )

    [4] => Array
        (
            [0] => 2013-01-07
            [1] => Test User
            [2] => 7:56
        )

    [5] => Array
        (
            [0] => 2013-01-08
            [1] => Test User
            [2] => 8:25
        )

    [6] => Array
        (
            [0] => 2013-01-09
            [1] => Test User
            [2] => 7:56
        )

    [7] => Array
        (
            [0] => 2013-01-10
            [1] => Test User
            [2] => 8:10
        )...

我想介绍一个方法来为 str_getcsv 函数添加参数。请尝试:$contents = Storage::get('folder/file.csv'); $filterCsv = function ($arg) { return str_getcsv($arg, ';', '"', '\r\n'); }; $mapped = array_map($filterCsv, preg_split('/\r*\n+|\r+/', $contents)); - Marlon Ferreira

2
那将会是这样的:
$parts = explode(PHP_EOL, $string);
$array = array();
foreach ($parts as $part) {
    $array[] = str_getcsv($part);
}

print_r($array);

如果 PHP_EOL 不起作用,尝试使用不同的分隔符 ("\n", "\r\n", "\r")。 - Maxim Khan-Magomedov

2
我使用以下方式将CSV文件数据转换为数组:-
ini_set('auto_detect_line_endings', TRUE);/// (PHP's detection of line endings) write at the top.


$csvrows = array_map('str_getcsv', file($filepath));
$csvheader = array_shift($csvrows);
$csv = array();
foreach ($csvrows as $row) {
   $csv[] = array_combine($csvheader, $row);
}

-1
 $path = 'uploads/my_csvfile.csv';
 $file_content = file_get_contents($path);
 $matches = array_map('str_getcsv', preg_split('/\r*\n+|\r+/',$file_content));
 print_r($matches);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接