Php 在一列中获取具有相同值的行
我从csv中获取的数据基于以下一些索引:Php 在一列中获取具有相同值的行,php,forms,csv,fgetcsv,Php,Forms,Csv,Fgetcsv,我从csv中获取的数据基于以下一些索引: $filename=$_FILES["file"]["tmp_name"]; if($_FILES["file"]["size"] > 0) { $file = fopen($filename, "r"); $rowCount = 0 ; $secondColumnArray = array(); while (($getData = fgetcs
$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);
}
这些是我的csv中的实际行检查实际csv中的第四列,过滤csv后,我得到了重复的行
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
重复的行数:
$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);
}
因此,我只想在第一列中列出具有最佳值的行以及如何获得这些值,请提供帮助。这里有一个重写:
你的意思是:如果$all_elements[0]==Bestand{//保留它}?我不确定是否要筛选if之后的行或foreach内部的行。仍然显示所有数据。请给我一些$getData[0]和$all_元素的示例。请将此信息直接编辑到您的问题中。以下是git上我的模块的完整代码:复制是问题我已将此数据从CSV插入到表中,因此它正在复制entries@Shoaib你是说csv文件有重复的行,你想过滤它们,使所有行都是唯一的吗?好像这是一个额外的步骤?@Shaoib请在屏幕上打印几个$getData数组,并将它们添加到您的问题中,然后我将能够完善您的流程并消除重复项。我还对您的GitHub文件提出了一些其他建议。
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
$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);
}