note 58980 modified in function.fgetcsv by danbrown

From: Date: Sun, 17 May 2009 18:15:26 +0000
Subject: note 58980 modified in function.fgetcsv by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155086@lists.php.net to get a copy of this message
<?php /** * Based on an example by ramdac at ramdac dot org * Returns a multi-dimensional array from a CSV file optionally using the * first row as a header to create the underlying data as associative arrays. * @param string $file Filepath including filename * @param bool $head Use first row as header. * @param string $delim Specify a delimiter other than a comma. * @param int $len Line length to be passed to fgetcsv * @return array or false on failure to retrieve any rows. */ function importcsv($file,$head=false,$delim=",",$len=1000) { $return = false; $handle = fopen($file, "r"); if ($head) { $header = fgetcsv($handle, $len, $delim); } while (($data = fgetcsv($handle, $len, $delim)) !== FALSE) { if ($head AND isset($header)) { foreach ($header as $key=>$heading) { $row[$heading]=(isset($data[$key])) ? $data[$key] : ''; } $return[]=$row; } else { $return[]=$data; } } fclose($handle); return $return; } ?> --was-- /** * Based on an example by ramdac at ramdac dot org * Returns a multi-dimensional array from a CSV file optionally using the * first row as a header to create the underlying data as associative arrays. * @param string $file Filepath including filename * @param bool $head Use first row as header. * @param string $delim Specify a delimiter other than a comma. * @param int $len Line length to be passed to fgetcsv * @return array or false on failure to retrieve any rows. */ function importcsv($file,$head=false,$delim=",",$len=1000) { $return = false; $handle = fopen($file, "r"); if ($head) { $header = fgetcsv($handle, $len, $delim); } while (($data = fgetcsv($handle, $len, $delim)) !== FALSE) { if ($head AND isset($header)) { foreach ($header as $key=>$heading) { $row[$heading]=(isset($data[$key])) ? $data[$key] : ''; } $return[]=$row; } else { $return[]=$data; } } fclose($handle); return $return; } http://php.net/manual/en/function.fgetcsv.php

« previous php.notes (#155086) next »