note 95132 modified in function.str-getcsv by danbrown

From: Date: Tue, 15 Dec 2009 03:27:37 +0000
Subject: note 95132 modified in function.str-getcsv by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-163981@lists.php.net to get a copy of this message
Here's a little function to convert a multi-line CSV string to an array: <?php function csv_to_array($csv, $delimiter = ',', $enclosure = '"', $escape = '\\', $terminator = "\n") { $r = array(); $rows = explode($terminator,trim($csv)); $names = array_shift($rows); $names = str_getcsv($names,$delimiter,$enclosure,$escape); $nc = count($names); foreach ($rows as $row) { if (trim($row)) { $values = str_getcsv($row,$delimiter,$enclosure,$escape); if (!$values) $values = array_fill(0,$nc,null); $r[] = array_combine($names,$values); } } return $r; } ?> --was-- Here's a little function to convert a multi-line CSV string to an array: function csv_to_array($csv, $delimiter = ',', $enclosure = '"', $escape = '\\', $terminator = "\n") { $r = array(); $rows = explode($terminator,trim($csv)); $names = array_shift($rows); $names = str_getcsv($names,$delimiter,$enclosure,$escape); $nc = count($names); foreach ($rows as $row) { if (trim($row)) { $values = str_getcsv($row,$delimiter,$enclosure,$escape); if (!$values) $values = array_fill(0,$nc,null); $r[] = array_combine($names,$values); } } return $r; } Here's an example of what you can do with a CSV dataset: http://raxanpdi.com/labs/pdi-demo/example.php?id=inventory http://php.net/manual/en/function.str-getcsv.php

« previous php.notes (#163981) next »