How to extract data from csv file in PHP

前端 未结 11 1300
醉酒成梦
醉酒成梦 2020-11-22 07:36

I have a csv file which looks like this

$lines[0] = \"text, with commas\", \"another text\", 123, \"text\",5;
$lines[1] = \"some without commas\", \"another          


        
11条回答
  •  渐次进展
    2020-11-22 08:30

    When you want to keep the index (first line) for multidimensional result array, you can use:

    $delim      = ';';
    $csvFile    = file($csv_file);
    $firstline  = str_getcsv($csvFile[0], $delim);
    $data       = array();
    foreach ($csvFile as $line) {
        $line   = str_getcsv($line, $delim);
        $data[] = array_combine($firstline, $line);
    }
    

提交回复
热议问题