php中去重二维数组

方法1:

$goods_terminal_unique = [];
$goods_terminal  = [['goods_code'=>1,'terminal_code'=>12],['goods_code'=>1,'terminal_code'=>23],['goods_code'=>1,'terminal_code'=>12]];
for ($i = 0; $i < count($goods_terminal); $i++) {
    $a = $goods_terminal[$i];
    unset($goods_terminal[$i]);
    if (!in_array($a, $goods_terminal_unique)) {
        $goods_terminal_unique[] = $a;
    }
}
var_dump($goods_terminal_unique);

方法2:降维(缺点:原有的键名会变为0,1,2,3…)

   $temp = [];
   $goods_terminal  = [['goods_code'=>1,'terminal_code'=>12],['goods_code'=>1,'terminal_code'=>23],
    foreach ($goods_terminal as $v) {
        $v = join(",", $v); //降维,也可以用implode,将一维数组转换为用逗号连接的字符串
        $temp[] = $v;
    }
    var_dump($temp);
    $temp = array_unique($temp);//去掉重复的字符串,也就是重复的一维数组
    foreach ($temp as $k => $v) {
        $temp[$k] = explode(",", $v);//再将拆开的数组重新组装
    }
    var_dump($temp);

你可能感兴趣的:(php,php)