2017-03-17 5 views
0

Je reçois des données d'un csv une base de un indice comme suit:Obtenez les lignes qui a les mêmes valeurs d'une colonne

$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); 
    } 

Dans l'exemple ci-dessus je reçois stat des données de l'indice 306, mais ce n'est pas une bonne approche pour moi je veux seulement obtenir ces rangs qui ont la valeur "Bestand" dans la première colonne.

Par exemple:

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

Ces sont les lignes réelles dans mes csv Vérifiez la quatrième colonne csv réelle et après le filtrage de la csv i obtenu les lignes en double.

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    "    

en double de lignes:

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 

Je veux juste les lignes qui a des valeurs « Bestand » dans leur première colonne Comment puis-je aider que s'il vous plaît.

+0

Voulez-vous dire: 'si (all_elements $ [0] == "Bestand") {// garder}'? Je ne suis pas sûr si vous voulez filtrer les lignes après le 'si 'ou dans le' foreach'. – mickmackusa

+0

Toujours afficher toutes les données .. – Shoaib

+0

s'il vous plaît montrez-moi quelques exemples de '$ getData [0]' et '$ all_elements'. Veuillez modifier cette information directement dans votre question. – mickmackusa

Répondre

1

Voici un rewrite:

$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); 
} 
+0

Ça fonctionne bien bro merci :) – Shoaib

+0

Mais maintenant, il montre les valeurs en double – Shoaib

+0

Cochez la question que j'ai mis à jour – Shoaib