note 95132 added to function.str-getcsv
| From: | Raymond at osu1 dot php dot net | Date: | Tue, 15 Dec 2009 01:49:06 +0000 |
| Subject: | note 95132 added to function.str-getcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163974@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:
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
----
Server IP: 190.15.66.25
Probable Submitter: 72.27.145.33
----
Manual Page -- http://www.php.net/manual/en/function.str-getcsv.php
Edit -- https://master.php.net/note/edit/95132
Del: integrated -- https://master.php.net/note/delete/95132/integrated
Del: useless -- https://master.php.net/note/delete/95132/useless
Del: bad code -- https://master.php.net/note/delete/95132/bad+code
Del: spam -- https://master.php.net/note/delete/95132/spam
Del: non-english -- https://master.php.net/note/delete/95132/non-english
Del: in docs -- https://master.php.net/note/delete/95132/in+docs
Del: other reasons-- https://master.php.net/note/delete/95132
Reject -- https://master.php.net/note/reject/95132
Search -- https://master.php.net/manage/user-notes.php