PHP - 无法将 CSV 字符串解析为数组

PHP - Unable to parse CSV String into array

我在将 CSV 字符串解析为多维数组时遇到问题。我已经看过这个问题 PHP CSV string to array 但我没有得到相同的结果..

打印在屏幕上的 CSV 如下所示:

Agent,Answered,Missed,Contribution,Per Hour,Total Ring Time,Mean Ring Time,Total Talk Time,Mean Talk Time,Total Wrap Time,Mean Wrap Time,Total Session Time,Mean Session Time,Number of Sessions,% Util,Agent Hang-Ups,Caller Hang-Ups,Agent Hang-Ups Percent,Caller Hang-Ups Percent Amber,16,0,2.0%,0.2,28.6,1.8,1861.1,116.3,0.0,0.0,234862.2,3403.8,69.0,0.8%,10,6,62.5%,37.5% Amie,106,0,13.5%,3.0,721.7,6.8,12268.0,115.7,0.0,0.0,127011.0,6350.5,20.0,9.7%,54,52,50.9%,49.1% Andrew S,4,0,0.5%,0.1,0.0,0.0,1495.6,373.9,46.9,11.7,216218.4,3793.3,57.0,0.7%,2,2,50.0%,50.0% Catherine,28,0,3.6%,0.7,55.3,2.0,3089.4,110.3,0.0,0.0,135318.2,1409.6,96.0,2.3%,14,14,50.0%,50.0% David S,63,0,8.0%,2.0,393.0,6.2,6429.6,102.1,0.0,0.0,113765.5,16252.2,7.0,5.7%,20,43,31.7%,68.3% James,10,0,1.3%,0.1,3.4,0.3,1796.5,179.7,0.0,0.0,258600.0,3747.8,69.0,0.7%,2,8,20.0%,80.0% Jo,20,0,2.5%,0.2,120.0,6.0,3753.3,187.7,0.0,0.0,454249.4,7699.1,59.0,0.8%,9,11,45.0%,55.0% Jodie,37,0,4.7%,0.7,39.9,1.1,3570.5,96.5,0.0,0.0,204273.4,3462.3,59.0,1.7%,21,16,56.8%,43.2% Joe,16,0,2.0%,0.3,15.2,1.0,1847.0,115.4,0.0,0.0,219218.5,4059.6,54.0,0.8%,3,13,18.8%,81.3% Josh,11,0,1.4%,0.2,9.7,0.9,1892.3,172.0,0.0,0.0,209009.6,2714.4,77.0,0.9%,2,9,18.2%,81.8% LINE,11,0,1.4%,0.0,0.0,0.0,0.1,0.0,0.0,0.0,0.0,0.0,0.0,0.0%,11,0,100.0%,0.0% Lee,67,0,8.5%,1.1,277.4,4.1,18616.5,277.9,44091.7,658.1,223320.1,5726.2,39.0,8.3%,45,22,67.2%,32.8% Local/5201@callback-agent,108,91,13.7%,0.3,672.7,3.4,11262.9,104.3,0.0,0.0,1209599.0,1209599.0,1.0,0.9%,35,73,32.4%,67.6% Local/5294@callback-agent,21,7,2.7%,0.1,161.7,5.8,1290.6,61.5,0.0,0.0,1209599.0,1209599.0,1.0,0.1%,11,10,52.4%,47.6% Local/5309@callback-agent,24,0,3.1%,0.1,0.0,0.0,4452.1,185.5,0.0,0.0,1209599.0,1209599.0,1.0,0.4%,8,16,33.3%,66.7% Local/5400@callback-agent,23,16,2.9%,1.5,208.8,5.4,997.3,43.4,0.0,0.0,54625.7,13656.4,4.0,1.8%,8,15,34.8%,65.2% Local/5402@callback-agent,12,0,1.5%,22.0,107.4,8.9,555.7,46.3,0.0,0.0,1960.0,1960.0,1.0,28.4%,5,7,41.7%,58.3% Local/5415@callback-agent,5,1,0.6%,0.0,76.6,12.8,221.2,44.2,0.0,0.0,0.0,0.0,0.0,0.0%,1,4,20.0%,80.0% Maddie,38,0,4.8%,0.6,91.0,2.4,5743.0,151.1,0.0,0.0,212379.5,2469.5,86.0,2.7%,9,29,23.7%,76.3% Mel,9,0,1.1%,0.2,4.4,0.5,1340.5,148.9,0.0,0.0,197666.8,3137.6,63.0,0.7%,4,5,44.4%,55.6% Paul M,56,0,7.1%,0.1,241.0,4.3,15575.1,278.1,0.0,0.0,3982747.0,3982747.0,1.0,0.4%,34,22,60.7%,39.3% Shane,13,0,1.7%,0.2,10.9,0.8,2244.3,172.6,0.0,0.0,214092.8,3398.3,63.0,1.0%,0,13,0.0%,100.0% Tony C,7,0,0.9%,0.1,36.2,5.2,1052.8,150.4,0.0,0.0,179391.7,1812.0,99.0,0.6%,3,4,42.9%,57.1% Vanessa,11,0,1.4%,0.0,56.3,5.1,608.8,55.3,0.0,0.0,1209599.0,1209599.0,1.0,0.1%,5,6,45.5%,54.5% Vic ,70,0,8.9%,0.0,486.2,6.9,9044.4,129.2,0.0,0.0,0.0,0.0,0.0,0.0%,27,43,38.6%,61.4% "

这看起来很像一个班轮。但是,当我将此数据导出到文件并使用 Excel 打开时,数据格式很好。

它在 Excel 中的样子

然而,在我可以将它发送到一个文件之前,我需要对其进行操作,为此我想将它更改为一个数组。但是什么时候做:

$test = explode(",", $csvString);
echo '<pre>'; print_r($test); echo '</pre>';

我得到的并不是我想要的。我想得到多维数组,但我得到的是一个单一的数组,它只是介于两者之间的名称。

输出旧方法:

Array
(
    [0] => Agent
    [1] => Answered
    [2] => Missed
    [3] => Contribution
    [4] => Per Hour
    [5] => Total Ring Time
    [6] => Mean Ring Time
    [7] => Total Talk Time
    [8] => Mean Talk Time
    [9] => Total Wrap Time
    [10] => Mean Wrap Time
    [11] => Total Session Time
    [12] => Mean Session Time
    [13] => Number of Sessions
    [14] => % Util
    [15] => Agent Hang-Ups
    [16] => Caller Hang-Ups
    [17] => Agent Hang-Ups Percent
    [18] => Caller Hang-Ups Percent
Amber
    [19] => 16
    [20] => 0
    [21] => 2.0%
    [22] => 0.2
    [23] => 28.6
    [24] => 1.8
    [25] => 1861.1
    [26] => 116.3
    [27] => 0.0
    [28] => 0.0
    [29] => 234862.2
    [30] => 3403.8
    [31] => 69.0
    [32] => 0.8%
    [33] => 10
    [34] => 6
    [35] => 62.5%
    [36] => 37.5%

输出第二种方法:

    // Curls stuff above, to access the file 
    // Curl accesses the correct path to the url and save is in a variable.
    // Session at this point is still open 

    $csvData = file_get_contents($url);
    $lines = explode(PHP_EOL, $csvData);
    $array = array();
    foreach ($lines as $line) {
        $array[] = str_getcsv($line);
    }
    var_dump($array);

    echo '<pre>'; print_r($array); echo '</pre>';

使用上述方法输出

    var_dump
    array(1) { [0]=> array(1) { [0]=> string(3) "" } }

    echo
    Array
    (
        [0] => Array
            (
                [0] => 
            )
     )

编辑 最后,在四处移动代码并仔细检查所有内容后,我发现我没有指向一个变量,而是指向一个方法...

预期产出

Array
(    
    [0] => Agent 
    (
        [0] => Answered
        [1] => Missed
        [2] => Contribution
        [3] => Per Hour
        [4] => Total Ring Time
        [5] => Mean Ring Time
        [6] => Total Talk Time
        [7] => Mean Talk Time
        [8] => Total Wrap Time
        [9] => Mean Wrap Time
        [10] => Total Session Time
        [11] => Mean Session Time
        [12] => Number of Sessions
        [13] => % Util
        [14] => Agent Hang-Ups
        [15] => Caller Hang-Ups
        [16] => Agent Hang-Ups Percent
        [17] => Caller Hang-Ups Percent
    )
    [1] => Amber 
    (
        [0] => 16
        [1] => 0
        [2] => 2.0%
        [3] => 0.2
        [4] => 28.6
        [5] => 1.8
        [6] => 1861.1
        [7] => 116.3
        [8] => 0.0
        [9] => 0.0
        [10] => 234862.2
        [11] => 3403.8
        [12] => 69.0
        [13] => 0.8%
        [14] => 10
        [15] => 6
        [16] => 62.5%
        [17] => 37.5%
    )
    .... ETC

您没有按照您引用的示例中显示的内容进行操作。在那个例子中,文件被读取,首先在 EOL 上展开以获得单独的行,然后每一行在','上单独展开以获得多维数组......该代码有什么问题?

仅仅通过“,”来解析 CSV 文件中的字段是完全错误的。这里面有很多细微差别。例如逗号可以嵌入字段中。