如何从PHP多维数组中删除重复值?

示例数组:

Array
(
    [0] => Array
    (
        [0] => abc
        [1] => def
    )

    [1] => Array
    (
        [0] => ghi
        [1] => jkl
    )

    [2] => Array
    (
        [0] => mno
        [1] => pql
    )

    [3] => Array
    (
        [0] => abc
        [1] => def
    )

    [4] => Array
    (
        [0] => ghi
        [1] => jkl
    )

    [5] => Array
    (
        [0] => mno
        [1] => pql
    )

)

当前回答

序列化和惟一的替代方案

$test = [ ['abc','def'], ['ghi','jkl'], ['mno','pql'], ['abc','def'], ['ghi','jkl'], ['mno','pql'], ]; $result = array_reduce( $test, function($carry,$item){ if(!in_array($item,$carry)) { array_push($carry,$item); } return $carry; }, [] ); var_dump($result); /* php unique.php array(3) { [0] => array(2) { [0] => string(3) "abc" [1] => string(3) "def" } [1] => array(2) { [0] => string(3) "ghi" [1] => string(3) "jkl" } [2] => array(2) { [0] => string(3) "mno" [1] => string(3) "pql" } } */

其他回答

一个简单的解决方案,可能不是最有效的:

function arrayUnique($myArray){
    if(!is_array($myArray))
        return $myArray;

    foreach ($myArray as &$myvalue){
        $myvalue=serialize($myvalue);
    }

    $myArray=array_unique($myArray);

    foreach ($myArray as &$myvalue){
        $myvalue=unserialize($myvalue);
    }

    return $myArray;

} 

对于长度不受限制的(n)维数组,尝试此解决方案 比如这个数组

$arr= [
0 => [0=>"a" , 1=>"b" ,  2=>"c" ] ,
1 => [0=>"x" , 1=>"b" , 2=>"a", 3=>"p"],
2=>   [
       [ 
          0=>"y" ,
          1=>"b" ,
          2=> [0=>"x" , 1=>"m" , 2=>"a"]
       ],
       1=>"z" ,
       2=>"v"
       ]
     ];

这就是答案

$ar2=[];
$ar3=[];
function test($arr){
    
    global $ar2,$ar3;
    if(is_array($arr)){
       return array_map("test",$arr);
    }
    if(!isset($ar2[$arr])){
        $ar2[$arr]=1;
        $ar3[]=$arr;
    }
}
array_map("test",$arr);
print_r($ar3);

另一种方式。也会保存密钥。

function array_unique_multidimensional($input)
{
    $serialized = array_map('serialize', $input);
    $unique = array_unique($serialized);
    return array_intersect_key($input, $unique);
}

我也遇到过类似的问题,但我找到了一个100%有效的解决方案。

<?php
    function super_unique($array,$key)
    {
       $temp_array = [];
       foreach ($array as &$v) {
           if (!isset($temp_array[$v[$key]]))
           $temp_array[$v[$key]] =& $v;
       }
       $array = array_values($temp_array);
       return $array;

    }


$arr="";
$arr[0]['id']=0;
$arr[0]['titel']="ABC";
$arr[1]['id']=1;
$arr[1]['titel']="DEF";
$arr[2]['id']=2;
$arr[2]['titel']="ABC";
$arr[3]['id']=3;
$arr[3]['titel']="XYZ";

echo "<pre>";
print_r($arr);
echo "unique*********************<br/>";
print_r(super_unique($arr,'titel'));

?>

唯一一个多维数组的简单而合理的方法如下:

如果你有一个这样的数组:

Array
(
    [Key1] => Array
        (
            [0] => Value1
            [1] => Value2
            [2] => Value1
            [3] => Value3
            [4] => Value1
        )
    [Key2] => Array
        (
            [0] => Value1
            [1] => Value2
            [2] => Value1
            [3] => Value3
            [4] => Value4
        )
)

使用foreach来解决这个问题:

foreach($array as $k=>$v){
    $unique=array_unique($v);
    $array[$k]=$unique;
}

它会给你以下结果:

Array
(
    [Key1] => Array
        (
            [0] => Value1
            [1] => Value2
            [3] => Value3
        )
    [Key2] => Array
        (
            [0] => Value1
            [1] => Value2
            [3] => Value3
            [4] => Value4
        )
)

如果你想重新排列键的顺序,

foreach($array as $k=>$v){
    $unique= array_values(array_unique($v));
    $array[$k]=$unique;
}

这个操作会给你这样排列的键值:

Array
(
    [Key1] => Array
        (
            [0] => Value1
            [1] => Value2
            [2] => Value3
        )
    [Key2] => Array
        (
            [0] => Value1
            [1] => Value2
            [2] => Value3
            [3] => Value4
        )
)

我希望这能澄清一切。