从php中的多维数组中删除重复值

问题描述:

I have csv file, i am reading data from it, i have join all which are randomly matching their rows like brands, customer and soon..,

Here i want to remove all duplicate from multi dimensional array which already in array

Here you can see how all duplicate values are appending to array in this image

Here is my php code

$csv = array_map('str_getcsv', file('test.csv'));
//echo '<pre>';
//print_r($csv);
//exit;
$brand =[];
foreach ($csv as $key => $value) {
    if(!(in_array($value[14], $brand))){
    $brand[$value[21]]['brands'][]=$value[14];    
    $brand[$value[21]]['products'][]=$value[1];    
    }   
}
echo '<pre>';
print_r($brand);

Thanks and welcome for all suggestions

我有csv文件,我正在从中读取数据,我已经加入所有随机匹配的行,如品牌 ,客户,很快......, p>

这里我要删除已经在数组中的多维数组中的所有重复 p>

在这里,您可以看到所有重复值如何附加到此图像中的数组 p>

  $ csv = array_map('str_getcsv',file('test.csv')); 
 // echo'&lt; pre&gt;  '; 
 // print_r($ csv); 
 // exit; 
 $ brand = []; 
foreach($ csv as $ key =&gt; $ value){
 if(!(in_array($) 价值[14],$ brand))){
 $ brand [$ value [21]] ['brands'] [] = $ value [14];  
 $ brand [$ value [21]] ['products'] [] = $ value [1];  
} 
} 
echo'&lt; pre&gt;'; 
print_r($ brand); 
  code>  pre> 
 
 

感谢并欢迎所有建议 p> \ n div>

You need to modify where you are looking in the in_array. You want to do this for both I'm sure, as well as check that it is set first:

foreach ($csv as $key => $value) {
    if(!isset($brand[$value[21]]['brands']) ||
       !in_array($value[14], $brand[$value[21]]['brands'])){
            $brand[$value[21]]['brands'][]=$value[14];
    }
    if(!isset($brand[$value[21]]['products']) ||
       !in_array($value[14], $brand[$value[21]]['products'])){
            $brand[$value[21]]['products'][]=$value[1];
    }            
}

array_unique() could do that for you.

http://php.net/manual/en/function.array-unique.php