How to extract data from csv file in PHP

前端 未结 11 1294
醉酒成梦
醉酒成梦 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:19

    In addition to Matt's suggestion, you can also use SplFileObject to read in the file:

    $file = new SplFileObject("data.csv");
    $file->setFlags(SplFileObject::READ_CSV);
    $file->setCsvControl(',', '"', '\\'); // this is the default anyway though
    foreach ($file as $row) {
        list ($fruit, $quantity) = $row;
        // Do something with values
    }
    

    source: http://de.php.net/manual/en/splfileobject.setcsvcontrol.php

提交回复
热议问题