If you want to convert comma separated values into an associated array, then use the following code.
Note: The code uses the first row of your CSV data to determine the keys in the associative array.
/** * Convert a comma separated file into an associated array. * The first row should contain the array keys. * * Example: * * @param string $filename Path to the CSV file * @param string $delimiter The separator used in the file * @return array * @link http://gist.github.com/385876 * @author Jay Williams <http://myd3.com/> * @copyright Copyright (c) 2010, Jay Williams * @license http://www.opensource.org/licenses/mit-license.php MIT License */ function csv_to_array($filename='', $delimiter=',') { if(!file_exists($filename) || !is_readable($filename)) return FALSE; $header = NULL; $data = array(); if (($handle = fopen($filename, 'r')) !== FALSE) { while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) { if(!$header) $header = $row; else $data[] = array_combine($header, $row); } fclose($handle); } return $data; }
Found here, courtesy of jaywilliams
it is a good for all data but i need only heading row array
Thank you very much, Eoin. This has saved me a lot of hassle.
Thank you so much for these few lines that have saved me hours!
Thank you very much, it was a quick help