PHP CSV string to array

前端 未结 10 1272
攒了一身酷
攒了一身酷 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:38

    Try this, it's working for me:

    $delimiter = ",";
      $enclosure = '"';
      $escape = "\\" ;
      $rows = array_filter(explode(PHP_EOL, $content));
      $header = NULL;
      $data = [];
    
      foreach($rows as $row)
      {
        $row = str_getcsv ($row, $delimiter, $enclosure , $escape);
    
        if(!$header) {
          $header = $row;
        } else {
          $data[] = array_combine($header, $row);
        }
      }
    

提交回复
热议问题