//读取csv文件
$data = file(ROOT_PATH.”gao/wnh.csv”);//csv文件路径
$field_list=array(‘cat_id’,’cat_name’,’cat_name_en’,’parent_id’,’filter_attr’);
foreach ($data as $line)
{
// 跳过第一行,如果不需要,则删去代码即可
if ($line_number == 0)
{
$line_number++;
continue;
}
// 转换编码
if (strpos(strtolower(EC_CHARSET), ‘utf’) === 0)
{
$line = ecs_iconv($_POST[‘charset’], ‘UTF8’, $line);
}
// 初始化
$arr = array();
$buff = ”;
$quote = 0;
$len = strlen($line);
for ($i = 0; $i < $len; $i++)
{
$char = $line[$i];
if (‘\\’ == $char)
{
$i++;
$char = $line[$i];
switch ($char)
{
case ‘”‘:
$buff .= ‘”‘;
break;
case ‘\”:
$buff .= ‘\”;
break;
case ‘,’;
$buff .= ‘,’;
break;
default:
$buff .= ‘\\’ . $char;
break;
}
}
elseif (‘”‘ == $char)
{
if (0 == $quote)
{
$quote++;
}
else
{
$quote = 0;
}
}
elseif (‘,’ == $char)
{
if (0 == $quote)
{
if (!isset($field_list[count($arr)]))
{
continue;
}
$field_name = $field_list[count($arr)];
$arr[$field_name] = trim($buff);
$buff = ”;
$quote = 0;
}
else
{
$buff .= $char;
}
}
else
{
$buff .= $char;
}
if ($i == $len – 1)
{
if (!isset($field_list[count($arr)]))
{
continue;
}
$field_name = $field_list[count($arr)];
$arr[$field_name] = trim($buff);
}
}
$goods_list[] = $arr;
}
最后print_r输出下可看到效果~~