Posted By

michielcox on 07/06/13


Tagged


Versions (?)

CSV to Array


 / Published in: PHP
 

  1. function csv_to_array($filename, $delimiter) {
  2. ini_set('auto_detect_line_endings', true);
  3.  
  4. if(!file_exists($filename) || !is_readable($filename)) {
  5. return FALSE;
  6. }
  7.  
  8. $header = NULL;
  9. $data = array();
  10. if (($handle = fopen($filename, 'r')) !== FALSE) {
  11. while (($row = fgetcsv($handle, $delimiter)) !== FALSE)
  12. {
  13. if(!$header) {
  14. $header = $row;
  15. } else {
  16. $data[] = array_combine($header, $row);
  17. }
  18. }
  19. fclose($handle);
  20. }
  21. return $data;
  22. }

Report this snippet  

You need to login to post a comment.