donk68254 2018-02-28 17:20
浏览 49
已采纳

从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

  • 写回答

2条回答 默认 最新

  • dongzhang0243 2018-02-28 17:35
    关注

    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];
        }            
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?