【问题标题】:Get the rows which has same values one column获取一列具有相同值的行
【发布时间】:2017-03-17 12:14:49
【问题描述】:

我从 csv 中获取数据,基于某些索引,如下所示:

$filename=$_FILES["file"]["tmp_name"];      


     if($_FILES["file"]["size"] > 0)
     {
        $file = fopen($filename, "r");
        $rowCount = 0 ;
        $secondColumnArray = array();

        while (($getData = fgetcsv($file,  100000, ",")) !== FALSE)
         {
         if($rowCount >=0){

             if(strpos($getData[0],"Bestand")!==false){

             array_push($secondColumnArray, $getData[0]);

               }
             foreach ($secondColumnArray as $all_elements){

                $all_elements_refine = explode(',', $all_elements);

                $new_refine =  $all_elements_refine[0];

                $all_elements_refine1 = explode(';', $new_refine);

                $specialChars = array(" ", "\r", "\n", '"', "*");
                $replaceChars = array("", "", "");
                $all_elements_refine2 = str_replace($specialChars, $replaceChars, $all_elements_refine1);
                 print_r($all_elements_refine2);

             }

         }
                 ++$rowCount;

         }

        //echo "<pre>"; print_r($secondColumnArray);   echo "</pre>"; 
         fclose($file); 
     }

在上面的示例中,我正在从索引 306 获取数据,但这对我来说不是一个好方法,我只想获取在第一列中具有“最佳”值的那些行。

例如:

Row 1:  Bestand , 1 , Hell , World
Row 2:  farb    , 2 , Hell , World
Row 3:  Bestand , 3 , Hell , World

这些是我的 csv 中的实际行检查实际 csv 中的第四列,在过滤 csv 后,我得到了重复的行。

Bestand   ;000000000;"I";00000000842143;000000000;00000095;009598;00000198;000000000;"MK Sterling mondstein         ";"moonstone           ";000000000;000000000;000000800;"20160525";"*           ";000000100;000000000;000008990;000008990;000008990;1900;"MK Sterling mondstein         "            
Bestand   ;000000000;"I";00000000842144;000000000;00000095;009598;00000198;000000000;"MK Sterling mondstein         ";"moonstone           ";000000000;000000000;000000800;"20160525";"*           ";000000100;000000000;000008990;000008990;000008990;1900;"MK Sterling mondstein         "            
Bestand   ;000000000;"I";00000000842148;000000000;00000095;009598;00000198;000000000;"MK Sterling mondstein         ";"moonstone           ";000000000;000000000;000000800;"20160525";"*           ";000000100;000000000;000008990;000008990;000008990;1900;"MK Sterling mondstein         "            
Bestand   ;000000000;"I";00000000842157;000000000;00000095;009598;00000198;000000000;"MK Sterling mondstein         ";"moonstone           ";000000000;000000000;000000800;"20160525";"*           ";000000100;000000000;000008990;000008990;000008990;1900;"MK Sterling mondstein         "            
Bestand   ;000000000;"I";00000000842158;000000000;00000095;009598;00000198;000000000;"MK Sterling mondstein         ";"moonstone           ";000000000;000000000;000000800;"20160525";"*           ";000000100;000000000;000008990;000008990;000008990;1900;"MK Sterling mondstein         "            
Bestand   ;000000000;"I";00000000842161;000000000;00000095;009598;00000198;000000000;"MK Sterling mondstein         ";"moonstone           ";000000000;000000000;000000800;"20160525";"*           ";000000100;000000000;000008990;000008990;000008990;1900;"MK Sterling mondstein         "            
Bestand   ;000000000;"I";00000000842162;000000000;00000095;009598;00000198;000000000;"MK Sterling mondstein         ";"moonstone           ";000000000;000000000;000000800;"20160525";"*           ";000000100;000000000;000008990;000008990;000008990;1900;"MK Sterling mondstein         "            
Bestand   ;000000000;"I";00000000842346;000000000;00000095;009598;00000198;000000000;"MK Sterling grün              ";"green zirconia      ";000000047;000000000;000000800;"20160525";"*           ";000000100;000000000;000008990;000008990;000008990;1900;"MK Sterling grün              "            
Bestand   ;000000000;"I";00000000842349;000000000;00000095;009598;00000198;000000000;"MK Sterling grün              ";"green zirconia      ";000000047;000000000;000000800;"20160525";"*           ";000000100;000000000;000008990;000008990;000008990;1900;"MK Sterling grün              "            

行重复:

id  A   B   C   D   E   F   G   H   I   J   K   L   M   N   O   P   Q   R   S   T   U   V   W
1   Bestand 000000000   I   00000000842143  000000000   00000095    009598  00000198    000000000   MKSterlingmondstein moonstone   000000000   000000000   000000800   20160525        000000100   000000000   000008990   000008990   000008990   1900    MKSterlingmondstein
2   Bestand 000000000   I   00000000842143  000000000   00000095    009598  00000198    000000000   MKSterlingmondstein moonstone   000000000   000000000   000000800   20160525        000000100   000000000   000008990   000008990   000008990   1900    MKSterlingmondstein
3   Bestand 000000000   I   00000000842144  000000000   00000095    009598  00000198    000000000   MKSterlingmondstein moonstone   000000000   000000000   000000800   20160525        000000100   000000000   000008990   000008990   000008990   1900    MKSterlingmondstein
4   Bestand 000000000   I   00000000842143  000000000   00000095    009598  00000198    000000000   MKSterlingmondstein moonstone   000000000   000000000   000000800   20160525        000000100   000000000   000008990   000008990   000008990   1900    MKSterlingmondstein
5   Bestand 000000000   I   00000000842144  000000000   00000095    009598  00000198    000000000   MKSterlingmondstein moonstone   000000000   000000000   000000800   20160525        000000100   000000000   000008990   000008990   000008990   1900    MKSterlingmondstein

所以我只想要在第一列中具有“最佳”值的行我怎么能得到请帮忙。

【问题讨论】:

  • 你的意思是:if($all_elements[0]=="Bestand"){ // keep it}?我不确定您是否要过滤 if 之后或 foreach 内的行。
  • 仍然显示所有数据..
  • 请给我看几个$getData[0]$all_elements 的样本。请将此信息直接编辑到您的问题中。

标签: php forms csv fgetcsv


【解决方案1】:

这里是重写:

$filename=$_FILES["file"]["tmp_name"];
if($_FILES["file"]["size"]>0){
    $file=fopen($filename,"r");
    $rowCount=0;
    $secondColumnArray=[];
    while(($getData=fgetcsv($file,100000))!==false){
        if($rowCount>=306){
            if(strpos($getData[0],"Bestand")!==false){
                array_push($secondColumnArray, $getData[0]);
            }
            foreach($secondColumnArray as $all_elements){
                print_r($all_elements);
            }
        }
        ++$rowCount;
    }
    fclose($file); 
}   

【讨论】:

  • 这是我在 git 上的模块的完整代码:github.com/shoaibgits/csv-by-row-to-database
  • 重复是问题
  • 我已将此数据从 CSV 插入到表中,因此它正在复制条目
  • @Shoaib 你是说 csv 文件有重复的行并且你想过滤它们以便所有行都是唯一的吗?好像这是一个额外的步骤?
  • @Shaoib 请在您的屏幕上打印几个 $getData 数组并将它们添加到您的问题中,然后我将能够改进您的流程并消除重复项。对于你的 GitHub 文件,我还有其他一些建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多