我有这个功能
function csv_to_array($filename='', $delimiter=',')
{
if(!file_exists($filename) || !is_readable($filename))
{
echo "Not a valid file name";
return FALSE;
}
$header = NULL;
$data = array();
$fname = file($filename);
$a1 = array('username','fullname','email','dept');
if (($handle = fopen($filename, 'r')) !== FALSE)
{
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
{
if(!$header)
{
$header = $row;
// print_r(array_diff_key($header,$array)) ;
}
else {
$data[] = array_combine($header, $row);
}
}
fclose($handle);
}
return $data;
}
它返回关联数组。 我的csv文件包含标题 用户名全名电子邮件部门 但我要求标题不要提及csv文件如何去做它
答案 0 :(得分:1)
怎么样:
$seen_header = false; // initialize to false.
$data = array();
// keep reading from the file till you have lines.
while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
// $seen_header will be false for the fist time.
if(!$seen_header) {
// don't add header to $data and make $seen_header true.
$seen_header = true;
}else{
// non header..add it.
$data[] = $row;
}
}