Process CSV Into Array With Column Headings For Key

后端 未结 9 2522
星月不相逢
星月不相逢 2020-11-29 03:35

I have a CSV with the first row containing the field names. Example data is...

\"Make\",\"Model\",\"Note\"
\"Chevy\",\"1500\",\"loaded\"
\"Chevy\",\"2500\",         


        
9条回答
  •  生来不讨喜
    2020-11-29 04:18

    Try this

    $csv = array_map("str_getcsv", file('file.csv', FILE_SKIP_EMPTY_LINES));    
    $header = array_shift($csv); // get header from array
    
    foreach ($csv as $key => $value) {    
        $csv[$key] = array_combine($header, $value);
        var_dump($csv[$key]['Model']);
    }
    
    var_dump($csv);
    

提交回复
热议问题