删除csv文件列中的重复项

时间:2017-07-15 09:01:02

标签: php csv duplicates

我有一个csv文件,可以获取汽车备件的数据,它对公司,汽车名称和型号有明显的意义,汽车名称列有重复,我试图使用一个功能删除它,但它删除了所有的行不是coulmn中的重复数据。 这是我的功能

    public function import(){
    $this->load->language('sale/orders');
    $json = array();
    $filename =  explode ('.xlsx',$this->request->post['filename']);
    $fullpath = DIR_DOWNLOAD . $this->request->post['filename']; //$filename[0] . ".xlsx";
    $json['full'] = $fullpath;        

        $file = "your-file.xls";
        $handle = fopen($fullpath, "r");
        $this->db->query("DELETE FROM `" . DB_PREFIX . "custom_orders_options`");
        $c = 0;                 
        while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
        {
        //$filesop = array_map("utf8_encode", $filesop); //added
        //$filesop = iconv( "Windows-1252", "UTF-8", $filesop );
        $company = $filesop[0];            
        $company_ar = iconv(mb_detect_encoding($filesop[1], mb_detect_order(), true), "UTF-8", $filesop[1]);            
        $car_name = $filesop[2];
        $car_name_ar = iconv(mb_detect_encoding($filesop[3], mb_detect_order(), true), "UTF-8", $filesop[3]);
        $model = $filesop[4];
        $model_ar = iconv(mb_detect_encoding($filesop[5], mb_detect_order(), true), "UTF-8", $filesop[5]);


        $query = $this->db->query("INSERT INTO `" . DB_PREFIX . "custom_orders_options` (company,car_name,model,company_ar,car_name_ar,model_ar) VALUES ('$company','$car_name','$model','$company_ar','$car_name_ar','$model_ar')");

        $c = $c + 1;            
        }

        if($query){
        $json['status'] = $this->language->get('success_import') .': '. $c;            
        }else{
        $json['status'] = $this->language->get('error_import');      
        }
    //var_dump($query);
    echo json_encode($json);
}

它适用于公司,但车名显示许多重复数据,所以任何人都可以帮忙吗?

0 个答案:

没有答案