PHP CSV string to array

前端 未结 10 1238
攒了一身酷
攒了一身酷 2020-11-27 02:41

I\'m trying to parse a CSV string to an array in PHP. The CSV string has the following attributes:

Delimiter: ,
Encl         


        
相关标签:
10条回答
  • 2020-11-27 03:21

    You should use fgetcsv. Since you cannot import a file as a stream because the csv is a variable, then you should spoof the string as a file by using php://temp or php://memory first:

    $fp = fopen("php://temp", 'r+');
    fputs($fp, $csvText);
    rewind($fp);
    

    Then you will have no problem using fgetcsv:

    $csv = [];
    while ( ($data = fgetcsv($fp) ) !== FALSE ) {
        $csv[] = $data;
    }
    

    $data will be an array of a single csv line (which may include line breaks or commas, etc), as it should be.

    0 讨论(0)
  • 2020-11-27 03:28

    Slightly shorter version, without unnecessary second variable:

    $csv = <<<'ENDLIST'
    "12345","Computers","Acer","4","Varta","5.93","1","0.04","27-05-2013"
    "12346","Computers","Acer","5","Decra","5.94","1","0.04","27-05-2013"
    ENDLIST;
    
    $arr = explode("\n", $csv);
    foreach ($arr as &$line) {
      $line = str_getcsv($line);
    }
    
    0 讨论(0)
  • 2020-11-27 03:32

    A modification of previous answers using array_map.
    Blow up the CSV data with multiple lines.

    $csv = array_map('str_getcsv', explode("\n", $csvData));
    
    0 讨论(0)
  • 2020-11-27 03:32

    If you need a name for the csv columns, you can use this method

     $example= array_map(function($v) {$column = str_getcsv($v, ";");return array("foo" => $column[0],"bar" => $column[1]);},file('file.csv'));
    
    0 讨论(0)
  • 2020-11-27 03:33

    If you have carriage return/line feeds within columns, str_getcsv will not work.

    Try https://github.com/synappnz/php-csv

    Use:

    include "csv.php";
    $csv = new csv(file_get_contents("filename.csv"));
    $rows = $csv->rows();
    foreach ($rows as $row)
    {
      // do something with $row
    }
    
    0 讨论(0)
  • 2020-11-27 03:35

    Do this:

    $csvData = file_get_contents($fileName);
    $lines = explode(PHP_EOL, $csvData);
    $array = array();
    foreach ($lines as $line) {
        $array[] = str_getcsv($line);
    }
    print_r($array);
    

    It will give you an output like this:

    Array
    (
        [0] => Array
            (
                [0] => 12345
                [1] => Computers
                [2] => Acer
                [3] => 4
                [4] => Varta
                [5] => 5.93
                [6] => 1
                [7] => 0.04
                [8] => 27-05-2013
            )
    
        [1] => Array
            (
                [0] => 12346
                [1] => Computers
                [2] => Acer
                [3] => 5
                [4] => Decra
                [5] => 5.94
                [6] => 1
                [7] => 0.04
                [8] => 27-05-2013
            )
    
    )
    

    I hope this can be of some help.

    0 讨论(0)
提交回复
热议问题