在PHP中从CSV创建多维关联数组

在PHP中从CSV创建多维关联数组,php,csv,multidimensional-array,associative-array,Php,Csv,Multidimensional Array,Associative Array,我试图在PHP中创建一个多维数组,其中内部数组与以下示例CSV字符串$CSV关联: # Results from 2015-06-16 to 2015-06-16. date,time,label,artist,composer,album,title,duration 2015-06-16,12:00 AM,Island,U2,"Clayton- Adam,The Edge,Bono,Mullen- Larry- Jr",Songs Of Innocence,SONG FOR SOMEONE,

我试图在PHP中创建一个多维数组,其中内部数组与以下示例CSV字符串$CSV关联:

# Results from 2015-06-16 to 2015-06-16.
date,time,label,artist,composer,album,title,duration
2015-06-16,12:00 AM,Island,U2,"Clayton- Adam,The Edge,Bono,Mullen- Larry- Jr",Songs Of Innocence,SONG FOR SOMEONE,03:46
2015-06-16,12:04 AM,Lowden Proud,"Fearing & White, Andy White, Stephen Fearing","White- Andy,Fearing- Stephen",Tea And Confidences,SECRET OF A LONG LASTING LOVE,03:10
2015-06-16,12:07 AM,Columbia,The Wallflowers,"Dylan- Jakob,Irons- Jack,Mathis- Stuart,Richling- Greg,Jaffee- Rami",Glad All Over,REBOOT THE MISSION,03:31
2015-06-16,12:10 AM,Distort Light,Bend Sinister,Moxon- Daniel,"Stories Of Brothers, Tales Of Lovers",JIMMY BROWN,03:48
第3+行格式之后的实际数据行数是可变的。到目前为止,我所做的是制作一个简单的多维数组:

$resultArray = str_getcsv($csv, PHP_EOL);//parse the rows
array_shift($resultArray);//shift out results first row: date info
array_shift($resultArray);//shift out results new first row: field labels
foreach($resultArray as &$row) {//parse the items in rows
    $row = str_getcsv($row, ",", '"');//removes the '"' field enclosure?
}//foreach
这是一个功能性多维数组,但我不知道如何使内部数组具有关联性,以便使用我预期使用的数组中的文本友好键访问它们:

$rowFieldKeysArray = array('date', 'time', 'label', 'artist', 'composer', 'album', 'title', 'duration');
我相信有一种简单的PHP方法可以使用键名数组作为关联数组的键,但我不知道如何做到这一点。我怀疑我需要做一些大致如下的事情:

foreach($resultArray as $rowKey => &$row) {
    $row[$rowFieldKeysArray[$rowKey]] = str_getcsv($row, ",", '"');
}//foreach
但这会产生一个“警告:非法字符串偏移量'date'[…]”

我该怎么做

编辑:根据Andrew评论中链接提供的综合信息和我接受的答案,我能够使用以下高效代码解决此问题:

    $resultArray = str_getcsv($csv, PHP_EOL);//parse the rows
    array_shift($resultArray);//shift out results first row: date info
    $rowFieldKeysArray = str_getcsv( array_shift($resultArray), "," );//shift out results new first row: field labels into field key name array
    //array('date', 'time', 'label', 'artist', 'composer', 'album', 'title', 'duration');//array of Key field names for associative array
    //       [0]     [1]      [2]      [3]        [4]        [5]      [6]       [7]      //key index
    foreach($resultArray as &$row) {//parse the items in rows
        $row = array_combine($rowFieldKeysArray, str_getcsv($row, ",", '"'));//array_combine replaces numeric indexes with key field labels
    }//foreach
谢谢大家!

这可能对你有帮助

脚本-用于从文件到阵列的csv

[akshay@localhost tmp]$ cat test.php
<?php

function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 0, $delimiter)) !== FALSE)
        {

            if(!$header)
            {
               $header = $row;
            }
            else
            {
                if(count($header)!=count($row)){ continue; }

                $data[] = array_combine($header, $row);
            }
        }
        fclose($handle);
    }
    return $data;
}

print_r(csv_to_array("/tmp/test.csv"));

?>
[akshay@localhost tmp]$ cat test.csv
date,time,label,artist,composer,album,title,duration
2015-06-16,12:00 AM,Island,U2,"Clayton- Adam,The Edge,Bono,Mullen- Larry- Jr",Songs Of Innocence,SONG FOR SOMEONE,03:46
2015-06-16,12:04 AM,Lowden Proud,"Fearing & White, Andy White, Stephen Fearing","White- Andy,Fearing- Stephen",Tea And Confidences,SECRET OF A LONG LASTING LOVE,03:10
2015-06-16,12:07 AM,Columbia,The Wallflowers,"Dylan- Jakob,Irons- Jack,Mathis- Stuart,Richling- Greg,Jaffee- Rami",Glad All Over,REBOOT THE MISSION,03:31
2015-06-16,12:10 AM,Distort Light,Bend Sinister,Moxon- Daniel,"Stories Of Brothers, Tales Of Lovers",JIMMY BROWN,03:48
两个脚本都将输出

[akshay@localhost tmp]$ php test.php
Array
(
    [0] => Array
        (
            [date] => 2015-06-16
            [time] => 12:00 AM
            [label] => Island
            [artist] => U2
            [composer] => Clayton- Adam,The Edge,Bono,Mullen- Larry- Jr
            [album] => Songs Of Innocence
            [title] => SONG FOR SOMEONE
            [duration] => 03:46
        )

    [1] => Array
        (
            [date] => 2015-06-16
            [time] => 12:04 AM
            [label] => Lowden Proud
            [artist] => Fearing & White, Andy White, Stephen Fearing
            [composer] => White- Andy,Fearing- Stephen
            [album] => Tea And Confidences
            [title] => SECRET OF A LONG LASTING LOVE
            [duration] => 03:10
        )

    [2] => Array
        (
            [date] => 2015-06-16
            [time] => 12:07 AM
            [label] => Columbia
            [artist] => The Wallflowers
            [composer] => Dylan- Jakob,Irons- Jack,Mathis- Stuart,Richling- Greg,Jaffee- Rami
            [album] => Glad All Over
            [title] => REBOOT THE MISSION
            [duration] => 03:31
        )

    [3] => Array
        (
            [date] => 2015-06-16
            [time] => 12:10 AM
            [label] => Distort Light
            [artist] => Bend Sinister
            [composer] => Moxon- Daniel
            [album] => Stories Of Brothers, Tales Of Lovers
            [title] => JIMMY BROWN
            [duration] => 03:48
        )

)

@安德鲁:你的推荐信很有帮助,我利用这些信息解决了这个问题。如果你将此作为答案发布,我将接受。请参考类似主题:由于某些原因无法将其作为答案发布。这一切都很好,我很高兴它有帮助。这是一个非常全面的答案,几乎是完美的,除了它可以在csv文件上工作,而不是像最初要求的那样在csv字符串上工作。我想我可以将它转换为使用str_getcsv而不是fgetcsv,所以我会将其标记为答案。是的,你可以这样转换,否则,请使用explode函数首先使用换行符,这将为您提供行数组,然后在每行上应用explode函数,这将为您提供列数组。目前,我正在移动互联网上,明天早上到达办公室时,我将更新StringThank的答案@Akshay。我在顶部编辑了我的问题,以显示适合我的解决方案。
[akshay@localhost tmp]$ php test.php
Array
(
    [0] => Array
        (
            [date] => 2015-06-16
            [time] => 12:00 AM
            [label] => Island
            [artist] => U2
            [composer] => Clayton- Adam,The Edge,Bono,Mullen- Larry- Jr
            [album] => Songs Of Innocence
            [title] => SONG FOR SOMEONE
            [duration] => 03:46
        )

    [1] => Array
        (
            [date] => 2015-06-16
            [time] => 12:04 AM
            [label] => Lowden Proud
            [artist] => Fearing & White, Andy White, Stephen Fearing
            [composer] => White- Andy,Fearing- Stephen
            [album] => Tea And Confidences
            [title] => SECRET OF A LONG LASTING LOVE
            [duration] => 03:10
        )

    [2] => Array
        (
            [date] => 2015-06-16
            [time] => 12:07 AM
            [label] => Columbia
            [artist] => The Wallflowers
            [composer] => Dylan- Jakob,Irons- Jack,Mathis- Stuart,Richling- Greg,Jaffee- Rami
            [album] => Glad All Over
            [title] => REBOOT THE MISSION
            [duration] => 03:31
        )

    [3] => Array
        (
            [date] => 2015-06-16
            [time] => 12:10 AM
            [label] => Distort Light
            [artist] => Bend Sinister
            [composer] => Moxon- Daniel
            [album] => Stories Of Brothers, Tales Of Lovers
            [title] => JIMMY BROWN
            [duration] => 03:48
        )

)