loading

magic_csv - load a CSV file into an associative array

Just pass this function the path to a CSV file and it'll return an array of key => value. magic_csv() takes the first row in the CSV file to be column (key) names.

If the function cannot open $filename, it'll return false. <?php

function magic_csv($filename){
 
$f = @fopen($filename,'r');
 if (!
$f) return false;
 
$headers fgetcsv($f,8090);
 
$all = array();
 while (!
feof($f)){
  
$values fgetcsv($f,8098);
  
$row = array();
  
$i=0;
  if (
is_array($values)){
     foreach(
$values as $v){
        if (
$i count($headers)) $row[$headers[$i]] = $v;
      
$i++;
     }
     
$all[] = $row;
    }
 }
 return 
$all;
}

?>
(unrated) | by defproc on December 21st, 2007

An account is required to post comments and adaptations

Ninjapad NX - The kung($foo) PHP Editor
RIP The Internet 1969-2012 | Powered by: Apache / PHP / MySQL | Made in Ninjapad NX | © defproc.co.uk, 2007_