note 35801 added to function.fgetcsv
| From: | NOSPAM-michael at mediaconcepts dot nl | Date: | Mon, 15 Sep 2003 11:45:49 +0000 |
| Subject: | note 35801 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-56518@lists.php.net to get a copy of this message | ||
A little contribution to make it more easy to use this function when working with a database. I
noticed this function doesn't add logical keys to the array, so I made a small function which
creates a 2dimensional array with the corresponding keys added to the rows.
// this function requires that the first line of your CSV file consists of the keys corresponding to
the values of the other lines
function convertCSVtoAssocMArray($file, $delimiter)
{
$result = Array();
$size = filesize($file) +1;
$file = fopen($file, 'r');
$keys = fgetcsv($file, $size, $delimiter);
while ($row = fgetcsv($file, $size, $delimiter))
{
for($i = 0; $i < count($row); $i++)
{
if(array_key_exists($i, $keys))
{
$row[$keys[$i]] = $row[$i];
}
}
$result[] = $row;
}
fclose($file);
return $result;
}
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35801
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35801&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35801&report=yes