array_unique

(PHP 4 >= 4.0.1, PHP 5, PHP 7, PHP 8)

array_unique移除数组中重复的值

说明

array_unique ( array $array , int $sort_flags = SORT_STRING ) : array

array_unique() 接受 array 作为输入并返回没有重复值的新数组。

注意键名保留不变。array_unique() 先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,接着忽略所有后面的键名。这并不意味着在未排序的 array 中同一个值的第一个出现的键名会被保留。

Note: 当且仅当 (string) $elem1 === (string) $elem2 时两个单元被认为相同。 例如,字符串表达一样时,会使用首个元素。

参数

array

输入的数组。

sort_flags

第二个可选参数sort_flags 可用于修改排序行为:

排序类型标记:

  • SORT_REGULAR - 按照通常方法比较(不修改类型)
  • SORT_NUMERIC - 按照数字形式比较
  • SORT_STRING - 按照字符串形式比较
  • SORT_LOCALE_STRING - 根据当前的本地化设置,按照字符串比较。

返回值

返回过滤后的数组。

更新日志

版本 说明
5.2.10 修改回 sort_flags 的默认值为 SORT_STRING
5.2.9 增加可选选项sort_flags,默认值 SORT_REGULAR。 5.2.9 之前,此函数内部使用 SORT_STRING 排序。

范例

Example #1 array_unique() 例子

<?php
$input 
= array("a" => "green""red""b" => "green""blue""red");
$result array_unique($input);
print_r($result);
?>

以上例程会输出:

Array
(
    [a] => green
    [0] => red
    [1] => blue
)

Example #2 array_unique() 和类型

<?php
$input 
= array(4"4""3"43"3");
$result array_unique($input);
var_dump($result);
?>

以上例程会输出:

array(2) {
  [0] => int(4)
  [2] => string(1) "3"
}

参见

注释

Note: 注意, array_unique() 不能应用于多维数组。

User Contributed Notes

thejimy007 at gmail dot com 06-Jul-2021 02:12
If you're looking for the fastest way to remove duplicate words in a string, because you may do so in the millions (because it doesn't matter if you do only some thousands) and use this code:

$result = implode(" ", array_unique( explode(" ", $string) ));

Then there is currently (2021) with the most recent PHP 7/8 versions only one faster alternative:

$result = preg_replace("/\b(\w+)\s+\\1\b/i", "$1", $string);

will remove all consecutive duplicate words and it's the fastest of all, taking only 30-40% of the time.

$result = preg_replace("/\b(\w+)\s+.*\\1\b/i", "$1", $string);

will remove all duplicate words anywhere in the string, requiring about 80%-85% of the time.

Of course, all speeds may vary depending on the length of your strings and their content. Please test for your case.
contact at evoweb dot fr 16-Feb-2021 08:32
Here is a solution to make unique values keeping empty values for an array with keys :

<?php
function array_unique_kempty($array) {
   
$values = array_unique($array);
   
$return = array_combine(array_keys($array), array_fill(0,count($array),null));
    return
array_merge($return,$values);
}

$myArray = [
   
"test1" => "aaa",
   
"test2" => null,
   
"test3" => "aaa",
   
"test4" => "bbb",
   
"test5" => null,
   
"test6" => "ccc",
   
"test7" => "ddd",
   
"test8" => "ccc"
];

echo
"<pre>".print_r(array_unique_kempty($myArray),true)."</pre>";

/*
Array
(
    [test1] => aaa
    [test2] =>
    [test3] =>
    [test4] => bbb
    [test5] =>
    [test6] => ccc
    [test7] => ddd
    [test8] =>
)
*/
?>
Eduardo Corts Vzquez (ecortes) 08-Jan-2021 10:19
Create multidimensional array unique for any single key index.

<?php

$users
= [
    [
'id' => 1, 'name' => 'ecortes', 'phone' => '1111111'],
    [
'id' => 2, 'name' => 'canepa', 'phone' => '222222'],
    [
'id' => 1, 'name' => 'john', 'phone' => '3333333'],
];

function
array_unique_multi($array, $key) {
    return
array_reduce($array, function($acc, $curr) use($key) {
        if(!
in_array($curr[$key], array_column($acc, $key))) {
           
array_push($acc, $curr);
        }
        return
$acc;
    }, []);
}

//Call the function
$unique_users = array_unique_multi($users, 'id');

//output
$users = [
    [
'id' => 1, 'name' => 'ecortes', 'phone' => '1111111'],
    [
'id' => 2, 'name' => 'canepa', 'phone' => '222222'],
];
?>
falundir at gmail dot com 19-Jun-2018 05:51
I find it odd that there is no version of this function which allows you to use a comparator callable in order to determine items equality (like array_udiff and array_uintersect). So, here's my version for you:

<?php
function array_uunique(array $array, callable $comparator): array {
   
$unique_array = [];
    do {
       
$element = array_shift($array);
       
$unique_array[] = $element;

       
$array = array_udiff(
           
$array,
            [
$element],
           
$comparator
       
);
    } while (
count($array) > 0);

    return
$unique_array;
}
?>

And here is a test code:

<?php
class Foo {

    public
$a;

    public function
__construct(int $a) {
       
$this->a = $a;
    }
}

$array_of_objects = [new Foo(2), new Foo(1), new Foo(3), new Foo(2), new Foo(2), new Foo(1)];

$comparator = function (Foo $foo1, Foo $foo2): int {
    return
$foo1->a <=> $foo2->a;
};

var_dump(array_uunique($array_of_objects, $comparator)); // should output [Foo(2), Foo(1), Foo(3)]
?>
subhrajyoti dot de007 at gmail dot com 01-Jun-2018 05:25
Simple and clean way to get duplicate entries removed from a multidimensional array.

<?php
          $multi_array
= $multi_array [0];
         
$multi_array = array_unique($multi_array);
         
print_r($multi_array);
?>
Matt S 26-Apr-2018 02:45
In case you are looking for a function that works like array_unique but does not do the string conversion first.

<?php
function array_unique_full($arr, $strict = false) {
    return
array_filter($arr, function($v, $k) use ($arr, $strict) {
        return
array_search($v, $arr, $strict) === $k;
    },
ARRAY_FILTER_USE_BOTH);
}
?>
Fabiano 04-Jan-2018 10:22
As for PHP 7.1.12, this is the comparison between array_keys(array_flip()), array_flip(array_flip()), for each elimination and array_unique. The array_keys(array_flip()) is the fastest method to remove duplication values from a single dimension array:

<?php

$max
= 1000000;
$arr = range(1,$max,3);
$arr2 = range(1,$max,2);
$arr = array_merge($arr,$arr2);

$time = -microtime(true);
$res1 = array_unique($arr);
$time += microtime(true);

echo
"<br>deduped to ".count($res1)." in ".$time;
// deduped to 666667 in 0.78185796737671
// memory used: 33558528

$time = -microtime(true);
$res2 = array_flip(array_flip($arr));
$time += microtime(true);

echo
"<br><br>deduped to ".count($res2)." in ".$time;
// deduped to 666667 in 0.072191953659058
// memory used: 3774873

$time = -microtime(true);
$res3 = array();
foreach(
$arr as $key=>$val) {
   
$res3[$val] = true;
}
$res3 = array_keys($res3);
$time += microtime(true);

echo
"<br /><br>deduped to ".count($res3)." in ".$time;
// deduped to 666667 in 0.095494985580444
// memory used: 33558528

$time = -microtime(true);
$res4 = array_keys(array_flip($arr));
$time += microtime(true);

echo
"<br /><br>deduped to ".count($res4)." in ".$time;
// deduped to 666667 in 0.05807900428772
// memory used: 33558528
calexandrepcjr at gmail dot com 20-Jun-2017 03:19
Following the Ghanshyam Katriya idea, but with an array of objects, where the $key is related to object propriety that you want to filter the uniqueness of array:

<?php
function obj_multi_unique($obj, $key = false)
    {
       
$totalObjs = count($obj);
        if (
is_array($obj) && $totalObjs > 0 && is_object($obj[0]) && ($key && !is_numeric($key))) {
            for (
$i = 0; $i < $totalObjs; $i++) {
                if (isset(
$obj[$i])) {
                    for (
$j = $i + 1; $j < $totalObjs; $j++) {
                        if (isset(
$obj[$j]) && $obj[$i]->{$key} === $obj[$j]->{$key}) {
                            unset(
$obj[$j]);
                        }
                    }
                }
            }
            return
array_values($obj);
        } else {
            throw new
Exception('Invalid argument or your array of objects is empty');
        }
    }
?>
stoff@ 20-Jan-2017 03:23
In reply to performance tests array_unique vs foreach.

In PHP7 there were significant changes to Packed and Immutable arrays resulting in the performance difference to drop considerably. Here is the same test on php7.1 here;
http://sandbox.onlinephpfunctions.com/code/2a9e986690ef8505490489581c1c0e70f20d26d1

$max = 770000; //large enough number within memory allocation
$arr = range(1,$max,3);
$arr2 = range(1,$max,2);
$arr = array_merge($arr,$arr2);

$time = -microtime(true);
$res1 = array_unique($arr);
$time += microtime(true);
echo "deduped to ".count($res1)." in ".$time;
// deduped to 513333 in 1.0876770019531

$time = -microtime(true);
$res2 = array();
foreach($arr as $key=>$val) {   
    $res2[$val] = true;
}
$res2 = array_keys($res2);
$time += microtime(true);
echo "<br />deduped to ".count($res2)." in ".$time;
// deduped to 513333 in 0.054931879043579
zoolyka at gmail dot com 18-Jan-2016 10:48
I found the simplest way to "unique" multidimensional arrays as follows:

<?php

$array
= array(
   
'a' => array(1, 2),
   
'b' => array(1, 2),
   
'c' => array(2, 2),
   
'd' => array(2, 1),
   
'e' => array(1, 1),
);

$array = array_map('json_encode', $array);
$array = array_unique($array);
$array = array_map('json_decode', $array);

print_r($array);

?>

As you can see "b" will be removed without any errors or notices.
tim at heuer dot nz 11-Oct-2015 10:57
I created a function for doing array_unique with a custom in_array function. So, if elements are considered in the array passed to this function, the array returned won't include elements that are considered equal and only return the values from the first array that are not considered in the array:

<?php

function uniqueBy(array $array, callable $in_array_func) {
   
$result = array();
    foreach (
$array as $key => $item) {
        if (!
$in_array_func($item, $result)) {
           
$result[$key] = $item;
        }
    }
    return
$result;
}

?>

Example usage:

<?php

$in_array_func
= function($item, $array) {
   
$fromBower = function($item) {
       
$result = substr($item, strpos($item, 'bower_components'));
        return
$result;
    };
    foreach (
$array as $myItem) {
        if (
$fromBower($item) == $fromBower($myItem)) {
            return
true;
        }
    }
    return
false;
};

// ...
$jss = uniqueBy($jss, $in_array_func);
?>
Ludovico Grossi 19-Dec-2014 02:01
[Editor's note: please note that this will not work well with non-scalar values in the array. Array keys can not be arrays themselves, nor streams, resources, etc. Flipping the array causes a change in key-name]

You can do a super fast version of array_unique directly in PHP, even faster than the other solution posted in the comments!

Compared to the built in function it is 20x faster! (2x faster than the solution in the comments).

<?php
function superfast_array_unique($array) {
    return
array_keys(array_flip($array));
}
?>

This works faster for small and big arrays.
Ghanshyam Katriya(anshkatriya at gmail) 09-Dec-2014 10:15
Create multidimensional array unique for any single key index.
e.g I want to create multi dimentional unique array for specific code

Code :
My array is like this,

<?php
$details
= array(
   
0 => array("id"=>"1", "name"=>"Mike",    "num"=>"9876543210"),
   
1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"),
   
2 => array("id"=>"1", "name"=>"Mathew""num"=>"784581254"),
);
?>

You can make it unique for any field like id, name or num.

I have develop this function for same :
<?php
function unique_multidim_array($array, $key) {
   
$temp_array = array();
   
$i = 0;
   
$key_array = array();
   
    foreach(
$array as $val) {
        if (!
in_array($val[$key], $key_array)) {
           
$key_array[$i] = $val[$key];
           
$temp_array[$i] = $val;
        }
       
$i++;
    }
    return
$temp_array;
}
?>

Now, call this function anywhere from your code,

something like this,
<?php
$details
= unique_multidim_array($details,'id');
?>

Output will be like this :
<?php
$details
= array(
   
0 => array("id"=>"1","name"=>"Mike","num"=>"9876543210"),
   
1 => array("id"=>"2","name"=>"Carissa","num"=>"08548596258"),
);
?>
mostafatalebi at rocketmail dot com 13-Dec-2013 02:38
If you find the need to get a sorted array without it preserving the keys, use this code which has worked for me:

<?php

$array
= array("hello", "fine", "good", "fine", "hello", "bye");

$get_sorted_unique_array = array_values(array_unique($array));

?>

The above code returns an array which is both unique and sorted from zero.
bx16soupapes at gmail dot com 11-Jun-2013 07:55
Another form to make an array unique (manual):

This is my array

Array
(
    [0] => Array
        (
            [0] => 40665
            [1] => 40665
            [2] => 40665
            [3] => 40665
            [4] => 40666
            [5] => 40666
            [6] => 40666
            [7] => 40666
            [8] => 40667
            [9] => 40667
            [10] => 40667
            [11] => 40667
            [12] => 40667
            [13] => 40668
            [14] => 40668
            [15] => 40668
            [16] => 40668
            [17] => 40668
            [18] => 40669
            [19] => 40669
            [20] => 40670
            [21] => 40670
            [22] => 40670
            [23] => 40670
            [24] => 40671
            [25] => 40671
            [26] => 40671
            [27] => 40671
            [28] => 40671
        )

    [1] => Array
        (
            [0] => 40672
            [1] => 40672
            [2] => 40672
            [3] => 40672
        )

)

this is my script:

    $anterior = 0;
    foreach($item as $array_key => $array_value)
    {
            echo "<li>$array_key";
            echo "<ul>";
            foreach($array_value as $xarray_key => $xarray_value){
                if($xarray_value != $anterior) {
                    echo "<li>$xarray_key => $xarray_value";
                    $item_nuevo[$array_key][] = $xarray_value;    // or to use the same key number $item_nuevo[$array_key][$xarray_key] = $xarray_value;               
                }
                $anterior = $xarray_value;
            }
            echo "</ul>";   
    }    

result:

0
    0 => 40665
    4 => 40666
    8 => 40667
    13 => 40668
    18 => 40669
    20 => 40670
    24 => 40671

1
    0 => 40672

saludos desde chile.
sashasimkin at gmail dot com 25-Apr-2012 01:26
My object unique function:

<?php
function object_unique( $obj ){
   
$objArray = (array) $obj;

   
$objArray = array_intersect_assoc( array_unique( $objArray ), $objArray );

    foreach(
$obj as $n => $f ) {
        if( !
array_key_exists( $n, $objArray ) ) unset( $obj->$n );
    }

    return
$obj;
}
?>

And these code:

<?php
class Test{
    public
$pr0 = 'string';
    public
$pr1 = 'string1';
    public
$pr2 = 'string';
    public
$pr3 = 'string2';
}

$obj = new Test;

var_dump( object_unique( $obj ) );
?>

returns:
object(Test)[1]
  public 'pr0' => string 'string' (length=6)
  public 'pr1' => string 'string1' (length=7)
  public 'pr3' => string 'string2' (length=7)
gma (at) qoob (dot) gr 12-Nov-2011 10:07
Lets say that you want to capture unique values from multidimensional arrays and flatten them in 0 depth.

i.e.
<?php
$tmp
= array( 'a' => array( 1,2,3,4 ), 'b' => array( 'c' => array( 4,5,6,7 ) ) );
?>

will return with array_flat( $tmp ) --> array( 1,2,3,4,5,6,7 );

I hope that the function will help someone

<?php
/**
 * @params      : $a            array           the recursion array
 *              : $s            array           storage array
 *              : $l            integer         the depth level
 *
 */
if( !function_exists( 'array_flat' ) )
{
    function
array_flat( $a, $s = array( ), $l = 0 )
    {
       
# check if this is an array
       
if( !is_array( $a ) )                           return $s;
       
       
# go through the array values
       
foreach( $a as $k => $v )
        {
           
# check if the contained values are arrays
           
if( !is_array( $v ) )
            {
               
# store the value
               
$s[ ]       = $v;
               
               
# move to the next node
               
continue;
               
            }
           
           
# increment depth level
           
$l++;
           
           
# replace the content of stored values
           
$s              = array_flat( $v, $s, $l );
           
           
# decrement depth level
           
$l--;
           
        }
       
       
# get only unique values
       
if( $l == 0 ) $s = array_values( array_unique( $s ) );
       
       
# return stored values
       
return $s;
       
    }
# end of function array_flat( ...
   
}
?>
Friendly Code 01-Aug-2011 03:48
I required a function that removed a specific duplicate entry from an array and ignoring all others so came up with this:

<?php
function specified_array_unique($array, $value)
{
   
$count = 0;
   
    foreach(
$array as $array_key => $array_value)
    {
        if ( (
$count > 0) && ($array_value == $value) )
        {
            unset(
$array[$array_key]);
        }
       
        if (
$array_value == $value) $count++;
    }
   
    return
array_filter($array);
}
?>
0cool.f 23-May-2011 03:54
Hope this can help...

<?php
function array_unique_key_group($array) {
    if(!
is_array($array))
        return
false;

   
$temp = array_unique($array);
    foreach(
$array as $key => $val) {
       
$i = array_search($val,$temp);
        if(!empty(
$i) && $key != $i) {
           
$temp[$i.','.$key] = $temp[$i];
            unset(
$temp[$i]);
        }
    }
    return
$temp;
}
?>

this function return an array that is unique, but preserve every key for the element...
sorry for bad english I'm italian...

$array['asd'] = 'value';
$array['lol'] = 'value';
$array['foo'] = 'val';
$array['bar'] = 'val';

var_dump(array_unique_key_group($array));
// will be output
array(2) { ["asd,lol"]=> string(5) "value" ["foo,bar"]=> string(3) "val" }
Anonymous 16-Jun-2010 05:46
It's often faster to use a foreache and array_keys than array_unique:

    <?php

    $max
= 1000000;
   
$arr = range(1,$max,3);
   
$arr2 = range(1,$max,2);
   
$arr = array_merge($arr,$arr2);

   
$time = -microtime(true);
   
$res1 = array_unique($arr);
   
$time += microtime(true);
    echo
"deduped to ".count($res1)." in ".$time;
   
// deduped to 666667 in 32.300781965256

   
$time = -microtime(true);
   
$res2 = array();
    foreach(
$arr as $key=>$val) {   
       
$res2[$val] = true;
    }
   
$res2 = array_keys($res2);
   
$time += microtime(true);
    echo
"<br />deduped to ".count($res2)." in ".$time;
   
// deduped to 666667 in 0.84372591972351

   
?>
regeda at inbox dot ru 12-Apr-2010 08:11
recursive array unique for multiarrays

<?php
function super_unique($array)
{
 
$result = array_map("unserialize", array_unique(array_map("serialize", $array)));

  foreach (
$result as $key => $value)
  {
    if (
is_array($value) )
    {
     
$result[$key] = super_unique($value);
    }
  }

  return
$result;
}
?>
brendel at krumedia dot de 24-Feb-2010 11:47
Prior to 5.2.9 you may create a list of unique objects this way:

<?php
for (; ; ) {
 
// ...
 
$uniqueObjectList[spl_object_hash($myObject)] = $myObject;
}
?>
amri [ at t] dhstudio dot eu 18-Dec-2009 02:36
I searched how to show only the de-duplicate elements from array, but failed.
Here is my solution:

<?php
function arrayUniqueElements($array)
{
return
array_unique(array_diff_assoc($array1,array_unique($array1)));
};
?>

Example:
<?php
$arr1
= array('foo', 'bar', 'xyzzy', '&', 'xyzzy',
'baz', 'bat', '|', 'xyzzy', 'plugh',
'xyzzy', 'foobar', '|', 'plonk', 'xyzzy',
'apples', '&', 'xyzzy', 'oranges', 'xyzzy',
'pears','foobar');

$result=arrayUniqueElements($arr1);
print_r($result);exit;
?>

Output:

Array
(
[4] => xyzzy
[12] => |
[16] => &
[21] => foobar
)
dirk dot avery a t gmail 30-Apr-2009 08:45
Although array_unique is not intended to work with multi-dimensional arrays, it does on 5.2.9.  However, it does not for 5.2.5.  Beware.
serg dot podtynnyi at gmail dot com 06-Feb-2009 11:21
//Remove duplicates from a text files and dump result in one file for example: emails list, links list etc

<?php

$data1
= file("data1.txt");
$data2 = file("data2.txt");

file_put_contents('unique.txt', implode('', array_unique(array_merge($data1,$data2))));
?>
jusvalceanu - SPAM at SPAM - yahoo dot com 06-Nov-2008 02:23
so .... my problem was multidimensional sort.

<?php
      $new
= array();
     
$exclude = array("");
      for (
$i = 0; $i<=count($attribs)-1; $i++) {
         if (!
in_array(trim($attribs[$i]["price"]) ,$exclude)) { $new[] = $attribs[$i]; $exclude[] = trim($attribs[$i]["price"]); }
      }

?>

Array $attribs is an array contaning arrays. Each array in the $attrib array consists in multiple fields (ex: name, lenght, price, etc.) to be more simpler in speech think that $attrib is the array resulted by a search sql query done by a visitator on your online shoopping website ... (so ... each array in the $attrib is a product :P) if you want to sort only the uniq results use the above or use this:

<?php
 
  
/* Our Array of products */
  
$attribs[] = array(
                          
"name"         => "Test Product 1",
                          
"length"     => "42 cm",
                          
"weight"     => "0,5 kg",
                          
"price"     => "10 $",
                          
"stock"     => "100",
                        );

  
$attribs[] = array(
                          
"name"         => "Test Product 2",
                          
"length"     => "42 cm",
                          
"weight"     => "1,5 kg",
                          
"price"     => "10 $",
                          
"stock"     => "200",
                        );

   
/* The nice stuff */

     
$new = array();
     
$exclude = array("");
      for (
$i = 0; $i<=count($attribs)-1; $i++) {
         if (!
in_array(trim($attribs[$i]["price"]) ,$exclude)) { $new[] = $attribs[$i]; $exclude[] = trim($attribs[$i]["price"]); }
      }
     
     
print_r($new); // $new is our sorted array

?>

Have fun tweaking this ;)) i know you will ;))

From Romania With Love
quecoder at gmail 25-Aug-2008 10:30
another method to get unique values is :

<?php
$alpha
=array('a','b','c','a','b','d','e','f','f');

$alpha= array_keys(array_count_values($alpha));

print_r($alpha);
?>

Output:
Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f )
Dorphalsig 28-Jul-2008 09:47
I had a problem with array_unique and multidimensional arrays ... Maybe there's a better way to do this, but this will work for any dimensional arrays.

<?php
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;

}
?>
PHP Expert 14-Apr-2008 05:34
Case insensitive for PHP v4.x and up.

<?php

function in_iarray($str, $a) {
    foreach (
$a as $v) {
        if (
strcasecmp($str, $v) == 0) {
            return
true;
        }
    }
    return
false;
}

function
array_iunique($a) {
   
$n = array();
    foreach (
$a as $k => $v) {
        if (!
in_iarray($v, $n)) {
           
$n[$k]=$v;
        }
    }
    return
$n;
}

$input = array("aAa","bBb","cCc","AaA","ccC","ccc","CCC","bBB","AAA","XXX");
$result = array_iunique($input);
print_r($result);

/*
Array
(
    [0] => aAa
    [1] => bBb
    [2] => cCc
    [9] => XXX
)
*/
?>
Ray dot Paseur at SometimesUsesGmail dot com 01-Mar-2008 07:46
I needed to identify email addresses in a data table that were replicated, so I wrote the array_not_unique() function:

<?php

function array_not_unique($raw_array) {
   
$dupes = array();
   
natcasesort($raw_array);
   
reset ($raw_array);

   
$old_key    = NULL;
   
$old_value    = NULL;
    foreach (
$raw_array as $key => $value) {
        if (
$value === NULL) { continue; }
        if (
$old_value == $value) {
           
$dupes[$old_key]    = $old_value;
           
$dupes[$key]        = $value;
        }
       
$old_value    = $value;
       
$old_key    = $key;
    }
return
$dupes;
}

$raw_array     = array();
$raw_array[1]    = 'abc@xyz.com';
$raw_array[2]    = 'def@xyz.com';
$raw_array[3]    = 'ghi@xyz.com';
$raw_array[4]    = 'abc@xyz.com'; // Duplicate

$common_stuff    = array_not_unique($raw_array);
var_dump($common_stuff);
?>
mnbayazit 28-Oct-2007 02:18
Case insensitive; will keep first encountered value.

<?php

function array_iunique($array) {
   
$lowered = array_map('strtolower', $array);
    return
array_intersect_key($array, array_unique($lowered));
}

?>
webmaster at jukkis dot net 30-Jul-2007 04:08
Another way to 'unique column' an array, in this case an array of objects:
Keep the desired unique column values in a static array inside the callback function for array_filter.

Example:
<?php
/* example object */
class myObj {
  public
$id;
  public
$value;
  function
__construct( $id, $value ) {
   
$this->id = $id;
   
$this->value = $value;
  }
}

/* callback function */
function uniquecol( $obj ) {
  static
$idlist = array();

  if (
in_array( $obj->id, $idlist ) )
    return
false;

 
$idlist[] = $obj->id;
  return
true;   
}

/* a couple of arrays with second array having an element with same id as the first */
$list  = array( new myObj( 1, ), new myObj( 2, 100 ) );
$list2 = array( new myObj( 1, 10 ), new myObj( 3, 100 ) );
$list3 = array_merge( $list, $list2 );

$unique = array_filter( $list3, 'uniquecol' );
print_r( $list3 );
print_r( $unique );

?>

In addition, use array_merge( $unique ) to reindex.
Ome_Henk 27-Oct-2006 12:16
For people looking at the flip flip method for getting unique values in a simple array. This is the absolute fastest method:

<?php
$unique
= array_keys(array_flip($array));
?>

It's marginally faster as:
<?php
$unique
= array_merge(array_flip(array_flip($array)));
?>

And it's marginally slower as:
<?php
$unique array_flip
(array_flip($array)); // leaves gaps
?>

It's still about twice as fast or fast as array_unique.

This tested on several different machines with 100000 random arrays. All machines used a version of PHP5.
geuis dot teses at gmail dot com 09-Oct-2006 10:27
Here's the shortest line of code I could find/create to remove all duplicate entries from an array and then reindex the keys.

<?php

// Fruits, vegetables, and other food:
$var = array('apple','banana','carrot','cat','dog','egg','eggplant','fish');

$var = array_values(array_unique($var));
?>
keneks at gmail dot com 30-Sep-2006 07:01
Taking the advantage of array_unique, here is a simple function to check if an array has duplicate values.

It simply compares the number of elements between the original array and the array_uniqued array.

<?php

function array_has_duplicates(array $array)
{
   
$uniq = array_unique($array);
    return
count($uniq) != count($array);
}

?>
uditsawhney at yahoo dot com 15-Jul-2006 12:14
<?php

//Fn for array_unique column-wise for multi-dimensioanl array without losing keys | Start
function array_uniquecolumn($arr)
{
   
$rows   = sizeof($arr);
   
$columns = sizeof($arr[0]);
   
   
$columnkeys = array_keys($arr[0]);
   

    for(
$i=0; $i<$columns; $i++)
    {
        for(
$j=0;$j<$rows;$j++)
        {
            for(
$k = $j+1; $k<$rows; $k++)
            {
                if(
$arr[$j][$columnkeys[$i]] == $arr[$k][$columnkeys[$i]])
                   
$arr[$k][$columnkeys[$i]] = "";       
            }
        }
   
    }

return (
$arr);

}
//Fn for array_unique column-wise for multi-dimensioanl array without losing keys | Stop

$arrUGCourse[]= array(  "CTR" => "1",

                       
"UGCOURSE"=>"ABC",

                       
"TSINITIATE"=>"540",

                       
"COUNT"=>"34",

                       
"ENTRY_DT"=>"2006-05-01",

                       
"CUMULATIVE"=> 44);

 

$arrUGCourse[]= array(  "CTR" => "2",

                       
"UGCOURSE"=>"ABC",

                       
"TSINITIATE"=>"5401",

                       
"COUNT"=>"341",

                       
"ENTRY_DT"=>"2006-05-11",

                       
"CUMULATIVE"=> 44);

print_r(array_uniquecolumn($arrUGCourse));

?>
agarcia at rsn dot com dot co 31-Mar-2006 12:41
This is a script for multi_dimensional arrays

<?php
function remove_dup($matriz) {
   
$aux_ini=array();
   
$entrega=array();
    for(
$n=0;$n<count($matriz);$n++)
    {
       
$aux_ini[]=serialize($matriz[$n]);
    }
   
$mat=array_unique($aux_ini);
    for(
$n=0;$n<count($matriz);$n++)
    {
       
           
$entrega[]=unserialize($mat[$n]);
       
    }
    return
$entrega;
}
?>
mcmeijer at yahoo dot com 27-Jan-2006 05:18
This is a recursive arrayUnique function for arrays of any dimension. (tested with 4-dimensional array)
The line '$newArray=deleteEmpty($newArray);' is optional and removes empty keys and values
<?php
function arrayUnique($myArray)
    {
   
$newArray = Array();
    if (
is_array($myArray))
        {
        foreach(
$myArray as $key=>$val)
            {
            if (
is_array($val))
                {
               
$val2 = arrayUnique($val);
                }
            else
                {
               
$val2 = $val;
               
$newArray=array_unique($myArray);
               
$newArray=deleteEmpty($newArray);
                break;
                }
            if (!empty(
$val2))
                {
               
$newArray[$key] = $val2;
                }
            }
        }
    return (
$newArray);
    }

function
deleteEmpty($myArray)
    {
   
$retArray= Array();
    foreach(
$myArray as $key=>$val)
        {
        if ((
$key<>"") && ($val<>""))
            {
           
$retArray[$key] = $val;
            }
        }
    return
$retArray;
    }
?>
memandeemail at gmail dot com 03-Jan-2006 10:47
Problem:
I have loaded an array with the results of a database
query.  The Fields are 'FirstName' and 'LastName'.

I would like to find a way to contactenate the two
fields, and then return only unique values for the
array.  For example, if the database query returns
three instances of a record with the FirstName John
and the LastName Smith in two distinct fields, I would
like to build a new array that would contain all the
original fields, but with John Smith in it only once.
Thanks for: Colin Campbell

Solution:

<?php
/**
 * The same thing than implode function, but return the keys so
 *
 * <code>
 * $_GET = array('id' => '4587','with' => 'key');
 * ...
 * echo shared::implode_with_key('&',$_GET,'='); // Resultado: id=4587&with=key
 * ...
 * </code>
 *
 * @param string $glue Oque colocar entre as chave => valor
 * @param array $pieces Valores
 * @param string $hifen Separar chave da array do valor
 * @return string
 * @author memandeemail at gmail dot com
 */
function implode_with_key($glue = null, $pieces, $hifen = ',') {
 
$return = null;
  foreach (
$pieces as $tk => $tv) $return .= $glue.$tk.$hifen.$tv;
  return
substr($return,1);
}

/**
 * Return unique values from a tree of values
 *
 * @param array $array_tree
 * @return array
 * @author memandeemail at gmail dot com
 */
function array_unique_tree($array_tree) {
 
$will_return = array(); $vtemp = array();
  foreach (
$array_tree as $tkey => $tvalue) $vtemp[$tkey] = implode_with_key('&',$tvalue,'=');
  foreach (
array_keys(array_unique($vtemp)) as $tvalue) $will_return[$tvalue] = $array_tree[$tvalue];
  return
$will_return;
}

$problem = array_fill(0,3,
array(
'FirstName' => 'John', 'LastName' => 'Smith')
);

$problem[] = array('FirstName' => 'Davi', 'LastName' => 'S. Mesquita');
$problem[] = array('FirstName' => 'John', 'LastName' => 'Tom');

print_r($problem);

print_r(array_unique_tree($problem));
?>
csaba at alum dot mit dot edu 09-Jun-2004 07:17
The following is an efficient, adaptable implementation of array_unique which always retains the first key having a given value:

<?php
function array_unique2(&$aray) {
   
$aHash = array();
    foreach (
$aray as $key => &$val) if (@$aHash[$val]++) unset ($aray[$key]);
}
?>

It is also adaptable to multi dimensional arrays.  For example, if your array is a sequence of (multidimensional) points, then in place of @$aHash[$val]++ you could use @$aHash[implode("X",$val)]++
If you want to not have holes in your array, you can do an array_merge($aray) at the end.

Csaba Gabor
spunk at dasspunk dot NOSPAM dot com 14-Nov-2001 04:52
I needed a way of retaining the original array's keys in the new, unique array. I came up with this. It works for my purposes but may need refinement.

<?php
function my_array_unique($somearray)
{
   
asort($somearray);
   
reset($somearray);
   
$currentarrayvar = current($somearray);
    foreach (
$somearray as $key=>$var)
    {
        if (
next($somearray) != $currentarrayvar)
        {
           
$uniquearray[$key] = $currentarrayvar;
           
$currentarrayvar = current($somearray);
        }
    }
   
reset($uniquearray);
    return
$uniquearray;
}
?>
PHP8中文手册 站长在线 整理 版权归PHP文档组所有