str_getcsv into a multidimensional array in php

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);
}

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);
}

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.

Tags:

Php

Csv