note 66674 added to function.fgetcsv
| From: | jon at jonhassall dot com | Date: | Thu, 25 May 2006 16:54:58 +0000 |
| Subject: | note 66674 added to function.fgetcsv | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-112277@lists.php.net to get a copy of this message | ||
I modified the code for my own purposes, to return an array with named keys for each field. I tried
various alternatives, and this seems to work well with exported Excel data.
<pre><?php
//Move through a CSV file, and output an associative array for each line
ini_set("auto_detect_line_endings", 1);
$current_row = 1;
$handle = fopen("testdatasource.csv", "r");
while ( ($data = fgetcsv($handle, 10000, ",") ) !== FALSE )
{
$number_of_fields = count($data);
if ($current_row == 1)
{
//Header line
for ($c=0; $c < $number_of_fields; $c++)
{
$header_array[$c] = $data[$c];
}
}
else
{
//Data line
for ($c=0; $c < $number_of_fields; $c++)
{
$data_array[$header_array[$c]] = $data[$c];
}
print_r($data_array);
}
$current_row++;
}
fclose($handle);
//Look at my photos www.jonhassall.com !
?></pre>
Jon Hassall
----
Server IP: 66.163.161.117
Probable Submitter: 62.254.0.18 (proxied: 82.16.0.200)
----
X-Spam-Status: No, hits=3.6 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_90_100,HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.fgetcsv.php
Edit -- http://master.php.net/note/edit/66674
Del: integrated -- http://master.php.net/note/delete/66674/integrated
Del: useless -- http://master.php.net/note/delete/66674/useless
Del: bad code -- http://master.php.net/note/delete/66674/bad+code
Del: spam -- http://master.php.net/note/delete/66674/spam
Del: non-english -- http://master.php.net/note/delete/66674/non-english
Del: in docs -- http://master.php.net/note/delete/66674/in+docs
Del: other reasons-- http://master.php.net/note/delete/66674
Reject -- http://master.php.net/note/reject/66674
Search -- http://master.php.net/manage/user-notes.php