str_getcsv into a multidimensional array in php

后端 未结 3 1368
忘掉有多难
忘掉有多难 2021-01-31 18:47

I have csv values like this:

$csv_data = \"test,this,thing
             hi,there,this
             is,cool,dude
             have,fun\";

I want

相关标签:
3条回答
  • 2021-01-31 19:26

    Assuming every row in the CSV data has the same number of columns, this should work.

    $lines = explode("\n", $csv_data);
    $head = str_getcsv(array_shift($lines));
    
    $array = array();
    foreach ($lines as $line) {
        $array[] = array_combine($head, str_getcsv($line));
    }
    

    If lines have a variable number of columns (as in your example, where the last line has 2 columns instead of 3), use this loop instead:

    foreach ($lines as $line) {
        $row = array_pad(str_getcsv($line), count($head), '');
        $array[] = array_combine($head, $row);
    }
    
    0 讨论(0)
  • 2021-01-31 19:26

    Here is a complete solution:

    $lines = explode("\n", $csv_data);
    $formatting = explode(",", $lines[0]);
    unset($lines[0]);
    $results = array();
    foreach ( $lines as $line ) {
       $parsedLine = str_getcsv( $line, ',' );
       $result = array();
       foreach ( $formatting as $index => $caption ) {
          if(isset($parsedLine[$index])) {
             $result[$formatting[$index]] = trim($parsedLine[$index]);
          } else {
             $result[$formatting[$index]] = '';
          }
       }
       $results[] = $result;
    }
    

    So what are we doing here?

    • First, your CSV data is split into array of lines with explode
    • Since the first row in your CSV describes data format, it must be separated from the actual data rows (explode and unset)
    • For storing the results, we initialize a new array ($results)
    • Foreach is used to iterate through the data line by line. For each line:
      • Line is parsed with PHP's str_getcsv
      • An empty result array is initialized
      • Each line is inspected in the light of the format. Cells are added and missing columns are padded with empty strings.
    0 讨论(0)
  • 2021-01-31 19:37

    Here is a very clean and simple solution:

    function parse_row($row) {
      return array_map('trim', explode(',', $row));
    }
    
    $rows   = str_getcsv($csv_data, "\n");
    $keys   = parse_row(array_shift($rows));
    $result = array();
    
    foreach ($rows as $row) {
      $row = parse_row($row);
      $row = array_pad($row, 3, NULL);
    
      $result[] = array_combine($keys, $row);
    }
    
    0 讨论(0)
提交回复
热议问题