How to extract data from csv file in PHP

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

    0 讨论(0)
  • 2020-11-22 08:23

    Suppose you have a create a function for same things, Then it should look like

    function csvtoarray($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, 1000, $delimiter)) !== FALSE)
            {   
                if(!$header){
                    $header = $row;
                }else{
                    $data[] = array_combine($header, $row);
                }
            }
            fclose($handle);
        }
        if(file_exists($filename)) @unlink($filename);
    
        return $data;
    }
    
    $data = csvtoarray('file.csv', ',');
    
    print_r($data);
    
    0 讨论(0)
  • You can use fgetcsv to parse a CSV file without having to worry about parsing it yourself.

    Example from PHP Manual:

    $row = 1;
    if (($handle = fopen("test.csv", "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);
            echo "<p> $num fields in line $row: <br /></p>\n";
            $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
            }
        }
        fclose($handle);
    }
    
    0 讨论(0)
  • /**
     * @return mixed[]
     */
    public function csvToArray(string $delimiter, string $filename = ''): array
    {
        $data = [];
        if (file_exists($filename) && is_readable($filename)) {
            $header = null;
    
            if (($handle = fopen($filename, 'r')) !== false) {
                while (($row = fgetcsv($handle, 1000, $delimiter)) !== false) {
                    if (!$header) {
                        $header = $row;
                    } else {
                        $data[] = array_combine($header, $row);
                    }
                }
                fclose($handle);
            }
        }
    
        return $data;
    }
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
提交回复
热议问题