note 78058 modified in function.fgetcsv by danbrown

From: Date: Sun, 17 May 2009 18:09:43 +0000
Subject: note 78058 modified in function.fgetcsv by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155070@lists.php.net to get a copy of this message
A much simpler way to map the heading/column names to the elements on each line. It also doesn't fill up one big array which could cause you to run out of memory on large datasets. This loads one at a time so you can process/insert to db/etc... <?php $handle = fopen('somefile.csv', 'r'); if ($handle) { set_time_limit(0); //the top line is the field names $fields = fgetcsv($handle, 4096, ','); //loop through one row at a time while (($data = fgetcsv($handle, 4096, ',')) !== FALSE) { $data = array_combine($fields, $data); } fclose($handle); } ?> --was-- A much simpler way to map the heading/column names to the elements on each line. It also doesn't fill up one big array which could cause you to run out of memory on large datasets. This loads one at a time so you can process/insert to db/etc... $handle = fopen('somefile.csv', 'r'); if ($handle) { set_time_limit(0); //the top line is the field names $fields = fgetcsv($handle, 4096, ','); //loop through one row at a time while (($data = fgetcsv($handle, 4096, ',')) !== FALSE) { $data = array_combine($fields, $data); } fclose($handle); } http://php.net/manual/en/function.fgetcsv.php

« previous php.notes (#155070) next »