(PHP 4 >= 4.0.1, PHP 5)
array_unique — Entfernt doppelte Werte aus einem Array
Erwartet ein array und gibt ein neues Array zurück, aus dem alle doppelten Einträge entfernt wurden.
Beachten Sie, dass Schlüssel bewahrt bleiben. Erst behandelt array_unique() die Werte als Strings und sortiert sie, danach wird der erste gefundene Schlüssel behalten, und alle folgenden Schlüssel ignoriert. Das heißt nicht, dass der Schlüssel des ersten zugehörigen Wertes aus dem unsortierten array behalten wird.
Hinweis: Zwei Elemente werden nur dann als gleich angesehen, wenn (string) $elem1 === (string) $elem2. In Worten: Wenn die String-Repräsentation die gleiche ist. Das erste Element wird verwendet.
Das Eingabe-Array.
Der optionale zweite Parameter sort_flags kann mit den folgenden Werten genutzt werden, um das Sortierverhalten zu ändern:
Sorting type flags:
Gibt das gefilterte Array zurück.
Version | Beschreibung |
---|---|
5.2.10 | Der Standardwert von sort_flags wurde auf SORT_STRING zurückgesetzt. |
5.2.9 | Der optionale Parameter sort_flags wurde mit dem Standardwert SORT_REGULA hinzugefügt. Vor 5.2.9 wurde die Sortierung intern mit SORT_STRING durchgeführt. |
Beispiel #1 array_unique()-Beispiel
<?php
$input = array("a" => "grün", "rot", "b" => "grün", "blau", "rot");
$result = array_unique($input);
print_r($result);
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
Array ( [a] => grün [0] => rot [1] => blau )
Beispiel #2 array_unique() und Typen
<?php
$input = array(4, "4", "3", 4, 3, "3");
$result = array_unique($input);
var_dump($result);
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
array(2) { [0] => int(4) [2] => string(1) "3" }
Hinweis: Beachten Sie, dass array_unique() nicht dazu gedacht ist, auf mehrdimensionalen Arrays zu arbeiten.
if you want to close the gaps into the keys after using array_unique() you can use array_values() afterwards. Example:
<?php
a = array("one", "two", "two", "three")
a = array_unique(a);
/* will lead to:
a[0] = "one"
a[1] = "two"
a[3] = "three"
*/
a = array_values(a);
/* Now we've got:
a[0] = "one"
a[1] = "two"
a[2] = "three"
*/
?>
Update, if you use SORT_NUMERIC on this kind of filtering it will be significantly faster.
However the array_flip method still is twice as fast.
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
?>
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;
}
?>
Prior to 5.2.9 you may create a list of unique objects this way:
<?php
for (; ; ) {
// ...
$uniqueObjectList[spl_object_hash($myObject)] = $myObject;
}
?>
I searched how to show only the duplicate elements from array, but failed.
Here is my solution:
<?php
function arrayDuplicate($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=arrayDuplicate($arr1);
print_r($result);exit;
?>
Output:
Array
(
[4] => xyzzy
[12] => |
[16] => &
[21] => foobar
)
This is how to merge 2 comma separated lists with unique value only.
<?php
$list1 = "4444, 5555, 6666";
$list2 = "4444, 5555, 7777";
// combine both lists with unique values only
$list3 = implode("," , array_unique(array_merge(explode(",",$list1),explode(",", $list2))));
echo $list3;
?>
The result is: 4444,5555,6666,7777
I'm not sure why you'd use any kind of loop to get the duplicates in an array. Here's a handy little function which does exactly that using some of PHP's other array_* functions.
<?php
function array_not_unique( $a = array() )
{
return array_diff_key( $a , array_unique( $a ) );
}
?>
Some arrays for testing:
<?php
$person = array();
$person[1] = "person_1";
$person[2] = "person_2";
$person[3] = "person_3";
$person[4] = "person_4";
$person[5] = "person_5";
$person[6] = "person_2"; # DUPE
$person[7] = "person_4"; # DUPE
$person[8] = "person_4"; # DUPE
echo '<pre>',print_r(array_not_unique($person),1),'</pre>';
$person = array();
$person[] = 1;
$person[] = 2;
$person[] = 3;
$person[] = 4;
$person[] = 5;
$person[] = 2; # DUPE
$person[] = 4; # DUPE
$person[] = 4; # DUPE
echo '<pre>',print_r(array_not_unique($person),1),'</pre>';
?>
Output:
Array
(
[6] => person_2
[7] => person_4
[8] => person_4
)
Array
(
[5] => 2
[6] => 4
[7] => 4
)
Hope it helps.
--Bill
array_unique() is case-sensitive. If you need a case insensitive version, you better use something like this :
$f = create_function('$a,$b', 'return (strtolower($a) > strtolower($b)?1:-1);');
usort( $your_array, $f );
One way to use array_unique() to handle the multidimensional array issue is to use an md5 hash of the serialized array as the key. I've seen some solutions listed here that stored the serialized string, but that requires unserialization, and moving data back and forth between two states for little reason is just a bad idea. Instead, you could try this method:
<?php
$values = array();
foreach($data as $d) {
$values[md5(serialize($d))] = $d;
}
sort($values);
?>
This will serialize and hash the value, and store the value with the hash as they key. This will ensure that each piece is only stored once, as the second attempt to store will just overwrite the first attempt with identical data.
The sort() at the end is a bit unnecessary, but I had to use it on my project, because the function I was passing the array to couldn't handle keys that weren't valid XML node names (and some md5 hash strings will start with numbers, which is invalid for XML).
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.
Let's say you have
<?php
$v=array("blue","blue","blue","blue");
if($v[0]==$v[1] && $v[1]==$v[2] && $v[2]==$v[3])
echo "Y"; else echo "N";//Y
?>
It works but the if-statement gets a bit messy if the number of array elements gets massive.
I find it is easier to use array_unique like so:
<?php
$v=array("blue","blue","blue","blue");
echo sizeof(array_unique($v))==1?"Y":"N";//Y
?>
I noticed there was no way to tell array_unique() to ignore certain duplicated keys, so I wrote the following. I imagine there's half a dozen more efficient ways to do this, but here goes:
<?php
$array = array('foo', 'bar', 'xyzzy', '&', 'xyzzy',
'baz', 'bat', '|', 'xyzzy', 'plugh',
'xyzzy', 'foobar', '|', 'plonk', 'xyzzy',
'apples', '&', 'xyzzy', 'oranges', 'xyzzy',
'pears');
$ignore_values = array('|', '&');
print_r(make_unique($array, $ignore_values));
function make_unique($array, $ignore)
{
while($values = each($array))
{
if(!in_array($values[1], $ignore))
{
$dupes = array_keys($array, $values[1]);
unset($dupes[0]);
foreach($dupes as $rmv)
{
unset($array[$rmv]);
}
}
}
return $array;
}
?>
OUTPUT:
Array
(
[0] => foo
[1] => bar
[2] => xyzzy
[3] => &
[5] => baz
[6] => bat
[7] => |
[9] => plugh
[11] => foobar
[12] => |
[13] => plonk
[15] => apples
[16] => &
[18] => oranges
[20] => pears
)
//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))));
?>
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
I came across one limitation of array_unique: it doesn't work properly if you have arrays inside your main array.
The reason is that to compare two values, the function tests if (string) $value1 == (string) $value2. So if $value1 and $value2 are both arrays, the function will evaluate the test to 'Array' == 'Array', and decide that the $values are repeated even if the arrays are different.
So a work around is to find a better conversion of an array to a string, which can be done with json:
<?php
print "define an array with repeated scalar '1' and repeated 'array(1)':";
$a_not_unique = array(
'a' => 1,
'b' => 1,
'c' => 2,
'd' => array(1),
'e' => array(1),
'f' => array(2),
);
print_r($a_not_unique);
print "try to use simply array_unique, which will not work since it exludes 'array(2)':";
$a_unique_wrong = array_unique($a_not_unique);
print_r($a_unique_wrong);
print "convert to json before applying array_unique, and convert back to array, which will successfully keep 'array(2)':";
$a_unique_right = $a_not_unique;
array_walk($a_unique_right, create_function('&$value,$key', '$value = json_encode($value);'));
$a_unique_right = array_unique($a_unique_right);
array_walk($a_unique_right, create_function('&$value,$key', '$value = json_decode($value, true);'));
print_r($a_unique_right);
?>
Results:
define an array with repeated scalar '1' and repeated 'array(1)':
Array
(
[a] => 1
[b] => 1
[c] => 2
[d] => Array
(
[0] => 1
)
[e] => Array
(
[0] => 1
)
[f] => Array
(
[0] => 2
)
)
try to use simply array_unique, which will not work since it exludes 'array(2)':
Array
(
[a] => 1
[c] => 2
[d] => Array
(
[0] => 1
)
)
convert to json before applying array_unique, and convert back to array, which will successfully keep 'array(2)':
Array
(
[a] => 1
[c] => 2
[d] => Array
(
[0] => 1
)
[f] => Array
(
[0] => 2
)
)
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 )
Here's another solution for returning an array that only includes repeated values. There is one given below but it only works on numerically indexed arrays; this one is more comprehensive since I used the foreach iterator. Also, this one preserves keys--in that the returned result contains a distinct list of repeats (storing only the first instance it encounters of each duplicate value).
<?php
function array_repeated($array)
{
if ( !is_array($array) ) {
return false;
}
$duplicates = array();
foreach ( $array as $key => $val ) {
end($array);
$k = key($array);
$v = current($array);
while ( $k !== $key ) {
if ( $v === $val ) {
$duplicates[$key] = $v;
break;
}
$v = prev($array);
$k = key($array);
}
}
return $duplicates;
}
?>
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;
}
?>
a lot of people create functions just to fix a notice error about an undefined index and removes blank array value.
why not using foreach instead of the for loop?
example:
<?php
foreach ($arrayname as $key => $value) {
//do what you want with $value withaout index stress
}
?>
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
)
*/
?>
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);
?>
To clarify the note above Example #1, the function appears to cast the elements to strings for comparison and then return the type of the first unique element encountered.
<?php
$input = array(0, 2, "2", 3, "9", 9);
$result = array_unique($input);
var_dump($result);
?>
array(4) {
[0]=>
int(0)
[1]=>
int(2)
[3]=>
int(3)
[4]=>
string(1) "9"
}
case insensitive
<?php
function array_iunique($array) {
return array_intersect_key($array,array_unique(
array_map(strtolower,$array)));
}
?>
<?php
function array_unique_FULL($array) {
foreach($array as $k => $v) {
if (is_array($v)) {
$ret = array_unique_FULL(array_merge($ret, $v));
} else {
$ret[$k] = $v;
}
} //for
return array_unique($ret);
}
?>
Array
(
[0] => js/pt.js
[1] => js/selectfile.js
[2] => js/pt.js
[3] => js/proyecto.js
[4] => Array
(
[0] => Array
(
[0] => js/selectfile.js
[1] => js/selectfile.js
[2] => js/y otro mas.js
)
[1] => q se yo
)
)
go array_unique_FULL()!!!!
Array
(
[0] => js/pt.js
[1] => js/selectfile.js
[3] => js/proyecto.js
[6] => js/y otro mas.js
[5] => q se yo
)
Smaller version for PHP5
<?php
# array array_unique_save (array array [, bool preserve_keys] )
function array_unique_save ($a, $pk = true) {
$a = array_diff_key($a, array_unique($a));
return ($pk ? $a : array_values($a));
}
?>
Should have thought of this sooner. x.x
<?php
function array_unique_save_php4 ($a, $pk = true) {
$t = array_keys(array_unique($a));
foreach ($t as $k) { unset($a[$k]); }
return ($pk ? $a : array_values($a));
}
?>
This is a solution to remove duplicate values from an array
<?php
$array[0] = "Yellow";
$array[1] = "Green";
$array[2] = "Yellow";
$array[3] = "Blue";
$array[4] = "Yellow";
$array = array_keys(array_flip($array));
//$array will output Yellow Green Blue
?>
Suggestion for being able to use array_unique on array of arrays/objects:
<?php
foreach ($arrayOfArrays as $key=>$value) {
$arrayOfArrays[$key] = "'" . serialize($value) . "'";
}
$arrayOfArrays = array_unique($arrayOfArrays);
foreach ($arrayOfArrays as $key=>$value) {
$arrayOfArrays[$key] = unserialize(trim($value, "'"));
}
?>
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, 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.
Another way to get unique objects/arrays:
<?php
function my_array_unique($array) {
return array_intersect_key($array, array_unique(array_map('serialize', $array)));
}
?>
This one would work with objects and arrays also.
<?php
function my_array_unique($array, $keep_key_assoc = false)
{
$duplicate_keys = array();
$tmp = array();
foreach ($array as $key=>$val)
{
// convert objects to arrays, in_array() does not support objects
if (is_object($val))
$val = (array)$val;
if (!in_array($val, $tmp))
$tmp[] = $val;
else
$duplicate_keys[] = $key;
}
foreach ($duplicate_keys as $key)
unset($array[$key]);
return $keep_key_assoc ? $array : array_values($array);
}
?>
An updated version of webcreators script that fixes a notice error about an undefined index and removes blank array value.
<?php
function my_array_unique($from) {
for ($i=count($from)-1;$i>1;$i--) {
$last = $from[$i];
$from[$i] = false;
if (!in_array($last,$from)) {
$from[$i]=$last;
}
}
$from = array_unique($from);
$from = array_slice($from,0,count($from)-1);
return $from;
}
?>
What if I only want the duplicated items, not the unique ones?
I tried to do it by using array_diff of an array and its unique, but it did not work (any help?); so I made this function with a relatively good efficiency:
<?php
function array_repeated($array) {
if(!is_array($array)) return false;
$repeated_values = Array();
$array_unique = array_unique($array);
if(count($array)-count($array_unique)) {
for($i=0;$i<count($array);$i++) {
if(!array_key_exists($i, $array_unique)) $repeated_values[] = $array[$i];
}
}
return $repeated_values;
}
?>
This function is based on the behaviour of the array_unique function that mantains the indexes of the array when deleting the item.
Note that array_unique preserves the keys, even if the array is not associative:
$values = Array("john@hotmail.com", "peter@hotmail.com", "will@hotmail.com", "john@hotmail.com", "laura@hotmail.com", "mariah@hotmail.com");
echo "<br>".count($values)." values.<br>";
var_dump($values);
$unique_values = array_unique($values);
echo "<br>".count($unique_values)." unique values.<br>";
var_dump($unique_values);
RESULT:
6 values.
array(6) {
[0]=>
string(16) "john@hotmail.com"
[1]=>
string(17) "peter@hotmail.com"
[2]=>
string(16) "will@hotmail.com"
[3]=>
string(16) "john@hotmail.com"
[4]=>
string(17) "laura@hotmail.com"
[5]=>
string(18) "mariah@hotmail.com"
}
5 unique values.
array(5) {
[0]=>
string(16) "john@hotmail.com"
[1]=>
string(17) "peter@hotmail.com"
[2]=>
string(16) "will@hotmail.com"
[4]=>
string(17) "laura@hotmail.com"
[5]=>
string(18) "mariah@hotmail.com"
}
$unique_values[3] is missing. Use array_merge or array_values if you need the index to be linear.
Just to note that array_unique, treats null values as none unique values. So if your using array_unique to detect duplicate values it will also detect multiple null values.
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.
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));
?>
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_search_dups($array)
{
$dup_array = $array;
$dup_array = array_unique($dup_array);
if(count($dup_array) != count($array))
{
return TRUE;
}
else
{
return FALSE;
}
}
?>
This function below will remove multiple values from array, remove 'empty' fields an also count how many times the given value occured.
it's rather slow and not 'elegant' but works.
usage:
<?php
$myarray = my_array_unique($myarray);
?>
in return it produces an 3 fields array:
1st is index number, 2nd is the value, 3rd is counter.
<?php
function my_array_unique($tablica)
{
$tnum=count($tablica);
$i=1;
$k=1;
$t[1]['product']="";
$t[1]['count']=1;
while($i<=$tnum) {
if (!array_multi_search($tablica[$i], $t)) {
$t[$k]['product']=$tablica[$i];
$t[$k]['count']=1;
$k++;
}
else {
$y=1;
while ($y<=count($t)) {
if ($t[$y]['product']==$tablica[$i])
$t[$y]['count']++;
$y++;
}
}
$i++;
}
$tablica=$t;
return $tablica;
}
?>
the function uses another function that i've found on the php.net site (i'm posting it only for informational reasons - i can't remember who wrote it):
<?php
function array_multi_search( $p_needle, $p_haystack )
{
if( !is_array( $p_haystack ) )
{
return false;
}
if( in_array( $p_needle, $p_haystack ) )
{
return true;
}
foreach( $p_haystack as $row )
{
if( array_multi_search( $p_needle, $row ) )
{
return true;
}
}
return false;
}
?>
[EDIT BY danbrown AT php DOT net: The array_multi_search() function was originally written by 'czeslaw' and posted to the in_array() function manual entry on 18 February, 2006.]
In addition to brettz9's remove_dups:
This one will actually take a multi-dimensional array and return it minus the duplicates in regard to the specified element of the inner arrays.
<?php
/*
Initialising new array to the first element of the given array.
Check whether current element in initial array has already been added to new array.
If yes break to save us some time. If no, then add current element to new array.
*/
function remove_dups($array, $row_element) {
$new_array[0] = $array[0];
foreach ($array as $current) {
$add_flag = 1;
foreach ($new_array as $tmp) {
if ($current[$row_element]==$tmp[$row_element]) {
$add_flag = 0; break;
}
}
if ($add_flag) $new_array[] = $current;
}
return $new_array;
} // end function remove_dups
?>
______________________________________________
EXAMPLE
<?php
$array[0] = array('Charles','Wade',233);
$array[1] = array('Charles','Watson',234);
$array[2] = array('Tom','Wade',235);
$array = remove_dups($array,0);
?>
The above example will output:
Array
(
[0] => Array ([0] => 'Charles' [1] => 'Wade' [2] => 233)
[1] => Array ([0] => 'Tom' [1] => 'Wade' [2] => 235)
)
<?php
remove dups($array,1);
?>
will output:
Array
(
[0] => Array ([0] => 'Charles' [1] => 'Wade' [2] => 233)
[1] => Array ([1] => 'Charles' [1] => 'Watson' [2] => 234)
)
At each case it removes all inner arrays that have a duplicate in the specified position and keeps the first inner array only.
<?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));
?>
I used the code submitted by "agarcia" regarding multi-dimensional arrays but keys with no data were still there. I've just added a line to completely unset the redondant part of the array so it is now acting like array_unique.
<?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]);
}
foreach ($entrega as $key => $row){
if (!is_array($row)) { unset($entrega[$key]); }
}
return $entrega;
}
?>
Thanks!
Davin Baragiotta
The shortest way i found to remove duplicate array from a column,
For example if you parse Multiple XML sources, you can remove duplicate items that contain the same link.
<?PHP
function remove_duplicate($array, $field)
{
foreach ($array as $sub)
$cmp[] = $sub[$field];
$unique = array_unique($cmp);
foreach ($unique as $k => $rien)
$new[] = $array[$k];
return $new;
}
?>
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;
}
?>
If you have a multi-dimensional array and wish to remove duplicates from a particular "column" as well as the corresponding values in the other "columns", you might find the following helpful.
<?php
function remove_dups($array, $index) {
$array_count = count($array);
$array_count_inner = count($array[$index]);
for ($i=0; $i<$array_count_inner; $i++) {
for ($j=$i+1; $j<$array_count_inner; $j++) {
if ($array[$index][$i]==$array[$index][$j]) {
for ($k=0; $k<$array_count; $k++) {
unset($array[$k][$i]);
} // end for
} // end if
} // end for
} // end for
return $array;
} // end function remove_dups
?>
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;
}
?>
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));
?>
array_unique function starts its comparation from beginning and pop the key off if there is more values inside array. The last one remains. But i needed to hold priority of the order of values and let the first one in.
Here is my easy solution:
<?php
function my_array_unique($from)
{
for ($i=count($from);$i>1;$i--)
{
$last = $from[$i];
$from[$i] = "";
if (!in_array($last,$from))
$from[$i]=$last;
}
return array_unique($from);
}
# One empty value remains in array.
# But its very easy to separate it while using output array.
?>
Yet another Array_Unique for multi-demensioned arrays. I've only tested this on two-demensioned arrays, but it could probably be generalized for more, or made to use recursion.
This function uses the serialize, array_unique, and unserialize functions to do the work.
<?php
function multi_unique($array) {
foreach ($array as $k=>$na)
$new[$k] = serialize($na);
$uniq = array_unique($new);
foreach($uniq as $k=>$ser)
$new1[$k] = unserialize($ser);
return ($new1);
}
?>
array_unique for multidimensional arrays. similar to the DISTINCT in SQL function.
the function can group, sum and count keys
<?PHP
/*
$array - nothing to say
$group_keys - columns which have to be grouped - can be STRING or ARRAY (STRING, STRING[, ...])
$sum_keys - columns which have to be summed - can be STRING or ARRAY (STRING, STRING[, ...])
$count_key - must be STRING - count the grouped keys
*/
function array_distinct ($array, $group_keys, $sum_keys = NULL, $count_key = NULL){
if (!is_array ($group_keys)) $group_keys = array ($group_keys);
if (!is_array ($sum_keys)) $sum_keys = array ($sum_keys);
$existing_sub_keys = array ();
$output = array ();
foreach ($array as $key => $sub_array){
$puffer = NULL;
#group keys
foreach ($group_keys as $group_key){
$puffer .= $sub_array[$group_key];
}
$puffer = serialize ($puffer);
if (!in_array ($puffer, $existing_sub_keys)){
$existing_sub_keys[$key] = $puffer;
$output[$key] = $sub_array;
}
else{
$puffer = array_search ($puffer, $existing_sub_keys);
#sum keys
foreach ($sum_keys as $sum_key){
if (is_string ($sum_key)) $output[$puffer][$sum_key] += $sub_array[$sum_key];
}
#count grouped keys
if (!array_key_exists ($count_key, $output[$puffer])) $output[$puffer][$count_key] = 1;
if (is_string ($count_key)) $output[$puffer][$count_key]++;
}
}
return $output;
}
?>
If you're doing numeric arrays etc. I found flip-flip to work much better than array_unique:
<?PHP
function microtime_float(){ //timing
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
//make an arry and fill it up
$final=array();
for($i=0;$i<50000;$i++){
$final[]=$i%13; //make sure there are some dupes
}
//try array unique
$start1 = microtime_float();
array_unique($final);
$stop1=microtime_float();
echo($stop1-$start1.'<br>');
//try my flip-flip
$start2=microtime_float();
array_flip(array_flip($final));
$stop2=microtime_float();
echo($stop2-$start2);
?>
Running this with only ints in the array (as above) I get runtimes such as:
1.6195669174194 (using unique)
0.017037868499756 (using flip flip)
which is two orders of magnitude faster!
Appending a string:
($final[]='test'.$i%13;)
gives:
0.42909598350525 (using unique)
0.023258924484253 (using flip-flip)
Which is not AS great, but still 20x faster than unique.
In both cases the flip-flip seems to use less memory than the unique.
Granted the flip-flip doesn't work for all cases, but if you're doing simple stuff like this, the flip-flip will give you better run times.
~JF
If the array key is a string it might be important to keep it although the value is the same (as I need it at the moment). So I wrote a function which also returns array elements which have the same value but different string keys.
<?php
function array_unique_numeric ($arr)
{
$str = $int = array();
foreach(array_keys($arr) as $key) {
${(is_int($key)?'int':'str')}[$key] = $arr[$key];
}
return array_merge($str, array_unique($int));
}
// typical array after an array_merge()...
$array = array("a" => "green", "b" => "brown", "c" => "blue", "red", "d" => "green", "yellow", "red");
print_r(array_unique($array));
// Array
// (
// [a] => green
// [b] => brown
// [c] => blue
// [0] => red
// [1] => yellow
// )
print_r(array_unique_numeric($array));
// Array
// (
// [a] => green
// [b] => brown
// [c] => blue
// [d] => green
// [0] => red
// [1] => yellow
// )
?>
<?php
/**
* Removes duplicate keys from an array
*
* @param array $array
* @return array
*/
function array_unique_key($array) {
$result = array();
foreach (array_unique(array_keys($array)) as $tvalue) {
$result[$tvalue] = $array[$tvalue];
}
return $result;
}
?>
A very simple way of getting rid of duplicate entries and re-indexing with key starting at 0:
<?php
$temp=array_unique($main);
$main=array_values($temp);
?>
Just a simple implementation for JavaScript:
<?php
function array_unique(thearray)
{
thearray.sort();
//reset($thearray);
newarray = new Array();
for (n=0;n<thearray.length;n++)
{
unique=1;//by default
for(nn=0;nn<newarray.length;nn++)
if (thearray[n] == newarray[nn])
{
unique=0;//already exists
break;
}
if(unique)//dont exists
newarray.push(thearray[n]);
}
return newarray;
}
?>
I quite like the following code for making multidimensional arrays unique:
<?php
foreach ($arrAddressList AS $key => $arrAddress) {
$arrAddressList[$key] = serialize($arrAddress);
}
$arrAddressList = array_unique($arrAdressList);
foreach ($arrAddressList AS $key => $strAddress) {
$arrAddressList[$key] = unserialize($strAddress);
}
?>
This gets me a unique array while not minding wether the the original array contains arrays or just strings (or whatever...).
OK, so here's my version that maintains index integrity:
<?php
function array_unique($array)
{
$out = array();
// loop through the inbound
foreach ($array as $key=>$value) {
// if the item isn't in the array
if (!in_array($value, $out)) {
// add it to the array
$out[$key] = $value;
}
}
return $out;
}
?>
Following the code copies of a little function I've wrote that actually works with multidimensional arrays.
It also resets the array indexes.
<?php
if ( !function_exists( "arrayUnique" ) ){
function arrayUnique ( $rArray ){
$rReturn = array ();
while ( list( $key, $val ) = each ( $rArray ) ){
if ( !in_array( $val, $rReturn ) )
array_push( $rReturn, $val );
}
return $rReturn;
}
}
?>
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
If you need to have the keys of the duplicates in an array returned, you may find this function useful:
<?php
function unique_events($array){
//checks $array for duplicate values and returns an
//array containing the keys of duplicates
$count= array_intersect_assoc($array, array_flip( array_count_values($array)));
foreach($array as $key=>$value){
if (in_array($value,$count)){
$return[$value][]=$key;
}
}
return $return;
}
?>
Example:
Input:
Array
(
[0] => 44
[1] => 23
[2] => 23
[3] => 23
[4] => 9
[5] => 9
[6] => 9
[7] => 9
[8] => 9
[9] => 9
[10] => 9
[11] => 9
)
Function returns:
Array
(
[23] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
[9] => Array
(
[0] => 4
[1] => 5
[2] => 6
[3] => 7
[4] => 8
[5] => 9
[6] => 10
[7] => 11
)
)
//Modify
Object Unique
<?php
class foo {
var $_name;
var $_age;
function foo($name,$age=NULL) { $this->_name = $name; $this->_age = $age; }
//function get() { return $this->_name; }
//function set($name,$age=NULL) { $this->_name = $name; $this->_age = $age; }
}
function DistinctOn ($obj, $item) {
$out = array();
$list = array();
foreach ($obj as $key=>$so) {
if (!in_array($so->$item, $list)) {
echo "key = $key,so = $so,item = $item,IFlist = ";print_r($list);echo "<br>";
$list[] = $so->$item;//°ËÁõ¹è¿
$out[$key] = $so;
}
echo "Forlist = ";print_r($list);echo "<br>";
}
return $out;
}
$foo_obj[0] = new foo('tom',20);
$foo_obj[1] = new foo('paul',66);
$foo_obj[2] = new foo('tom',23);
$item = '_name';
$result = DistinctOn ($foo_obj, $item);
while(list($k,$v) = each($result)) {
print "K = ";print_r($k);
print ",V = ";print_r($v);
print "<br>";
}
//key = 0,so = Object,item = _name,IFlist = Array ( ) ,Forlist = Array ( [0] => tom )
//key = 1,so = Object,item = _name,IFlist = Array ( [0] => tom ) ,Forlist = Array ( [0] => tom [1] => paul )
//Forlist = Array ( [0] => tom [1] => paul )
//K = 0,V = foo Object ( [_name] => tom [_age] => 20 )
//K = 1,V = foo Object ( [_name] => paul [_age] => 66 )
print "<br><br>";
$item = '_age';
$result = DistinctOn ($foo_obj, $item);
while(list($k,$v) = each($result)) {
print "K = ";print_r($k);
print ",V = ";print_r($v);
print "<br>";
}
//key = 0,so = Object,item = _age,IFlist = Array ( ) ,Forlist = Array ( [0] => 20 )
//key = 1,so = Object,item = _age,IFlist = Array ( [0] => 20 ) ,Forlist = Array ( [0] => 20 [1] => 66 )
//key = 2,so = Object,item = _age,IFlist = Array ( [0] => 20 [1] => 66 ) ,
// Forlist = Array ( [0] => 20 [1] => 66 [2] => 23 )
//K = 0,V = foo Object ( [_name] => tom [_age] => 20 )
//K = 1,V = foo Object ( [_name] => paul [_age] => 66 )
//K = 2,V = foo Object ( [_name] => tom [_age] => 23 )
?>
this simple function is similar based on to the function unique_multi_array posted by tru at ascribedata dot com, but will work for objects rather than arrays. use 2d objects. Its eg useful to work with mysql queries since in recent versions, the DISTINCT option in mysql selects will only work when selecting a single (not more) row.
eg.
foo_obj[0]: name: 'tom', age: '20'
foo_obj[1]: name: 'paul', age: '66'
foo_obj[2]: name: 'tom', age: '23'
foo_obj = DistinctOn ($foo_obj, $item) will return
foo_obj[0]: name: 'tom', age: '20'
foo_obj[1]: name: 'paul', age: '66'
<?php
function DistinctOn ($obj, $item) {
$out = array();
$list = array();
foreach ($obj as $key=>$so) {
if (!in_array($so->$item, $list)) {
$list[] = $so->$item;
$out[$key] = $so;
}
}
return $out;
}
?>
Here's a function to make a multi-dimensional array have only DISTINCT values for a certain "column". It's like using the DISTINCT parameter on a SELECT sql statement.
<?php
function unique_multi_array($array, $sub_key) {
$target = array();
$existing_sub_key_values = array();
foreach ($array as $key=>$sub_array) {
if (!in_array($sub_array[$sub_key], $existing_sub_key_values)) {
$existing_sub_key_values[] = $sub_array[$sub_key];
$target[$key] = $sub_array;
}
}
return $target;
}
?>
this function will return an array with unique value and proper key increment start from 0.
<?php
/*******************************/
function my_array_unique($somearray){
$tmparr = array_unique($somearray);
$i=0;
foreach ($tmparr as $v) {
$newarr[$i] = $v;
$i++;
}
return $newarr;
}
/********************************/
?>
eg:
<?php
$foo_arr[0] ='aa'
$foo_arr[1] ='bb'
$foo_arr[2] ='cc'
$foo_arr[3] ='bb'
$foo_arr[4] ='aa'
$foo_arr[5] ='dd'
?>
normal array_unique will return:
<?php
$foo_arr[0] ='aa';
$foo_arr[1] ='bb';
$foo_arr[2] ='cc';
$foo_arr[3] ='';
$foo_arr[4] ='';
$foo_arr[5] ='dd'
?>
my_array_unique will return:
<?php
$foo_arr[0] ='aa';
$foo_arr[1] ='bb';
$foo_arr[2] ='cc';
$foo_arr[3] ='dd'
?>
To get a list of the duplicated values in an array, array_unique isn't much help. Instead, use array_filter in conjunction with a callback function, as below:
<?php
$checkKeysUniqueComparison = create_function('$value','if ($value > 1) return true;');
$result = array_keys (array_filter (array_count_values($array), $checkKeysUniqueComparison));
?>
These two lines therefore will create $result, an array of duplicated values in the array $array, once each. E.g. the array
$array = array ("a", "b", "a", "b", "x", "y", "z", "x");
gives the result
Array([0] => a [1] => b [2] => x)
in addition to bisqwit:
in some cases you can simply use serialize instead of recursivemakehash().
<?php
function array_unique2($input) {
$tmp = array();
foreach($input as $a => $b)
$tmp[$a] = serialize($b);
$newinput = array();
foreach(array_unique($tmp) as $a => $b)
$newinput[$a] = $input[$a];
return $newinput;
}
?>
Have fun
Juggler
Before I found the mysql distinct I had to make a nicer array from the keys/values that I got from array_unique so.
<?php
$groups=array_unique($groups);
$newgroup[0]=reset($groups);
for ($x=1;$x<sizeof($groups);$x++)
{
$newgroup[$x]=next($groups);
}
?>
Try this:
array_flip(array_flip($array));
It gives the same result as the old array_unique()
<?php
$truc = array("l810u00","l810u00","l810q00");
$machin = array_unique($truc);
for($i=0;$i < count($machin) ; $i++){
print $machin[$i]."
";
}
?>
result :
l810u00
This is not strange: $machin (as returned by array unique), contains "l810u00" either in key[0] or key[1] but not both (the key depends on the ersion of PHP), and "l810q00" in key[2].
The returned array has TWO elements so count($machin)==2.
The returned array has a hole in it, and you're not displaying its full content. You could verify it by using this display loop instead:
foreach($machine as $key=>$value){
print '[' . $key . '] => ' . $value . '
";
}
result:
[0] => l810q00
[2] => l810u00
(the first line may display [1] instead of [0] for PHP 4.0.1p3, but you'll get the same order of values and two lines, as expected). When calling array_values() on the result, you're building a new array with the same values in the same order, but with renumbered keys (without holes in numeric keys).
array_unique() doesn't return anything useful if your input is an
array containing arrays. This function overcomes the problem.
Any level of recursions is possible here. If the input is a plain
array, the result is compatible with array_unique() result.
<?php
function recursivemakehash($tab)
{
if(!is_array($tab))
return md5($tab);
$p = '';
foreach($tab as $a => $b)
$p .= sprintf('%08X%08X', crc32($a), crc32(recursivemakehash($b)));
return $p;
}
function array_unique2($input)
{
$dumdum = array();
foreach($input as $a => $b)
$dumdum[$a] = recursivemakehash($b);
$newinput = array();
foreach(array_unique($dumdum) as $a => $b)
$newinput[$a] = $input[$a];
return $newinput;
}
?>
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;
}
?>
>array_unique() will keep the first key encountered
>for every value, and ignore all following keys.
This is true only in PHP 4.0.4pl1, i.e. the code below:
<?php
$temp = Array ('pl', 'pl', 'en', 'en', 'cz', 'de', 'en', 'pl');
print_r($temp);
print_r(array_unique($temp));
?>
Will produce this output:
Array
(
[0] => pl
[1] => pl
[2] => en
[3] => en
[4] => cz
[5] => de
[6] => en
[7] => pl
)
Array
(
[0] => pl
[2] => en
[4] => cz
[5] => de
)
And in PHP 4.0.3pl1 will produce this output:
Array
(
[0] => pl
[1] => pl
[2] => en
[3] => en
[4] => cz
[5] => de
[6] => en
[7] => pl
)
Array
(
[4] => cz
[5] => de
[6] => en
[7] => pl
)
This little function resolves the problem:
<?php
function my_array_unique(&$old){
$new = array();
foreach($old as $key => $value){
if(!in_array($value, $new)) $new[$key] = $value;
}
return $new;
}
?>
Attention!
If you use array_unique be aware of data-types! (I spent hours of debugging because of that ...).
For example, if you've got an array containing a '3' as number and another '3' as string it won't be eliminated by array_unique.
An Example where this can happen, without really thinking about it:
I've got an article-list with product-numbers where the third and fourth digit is the code for the producer. So I read in the file an process it line by line and put each producer-code into an array:
------------------------------
<?php
$i=0;
while($line = fgets($csv, 10000) {
// splitting the line, product_no is the first part:
$data = explode(";", $line);
// putting the producer_code into an array:
$producer_id[$i] = trim(substr($data[0], 2, 2));
// make a special exception:
if(trim(substr($data[0], 2, 2)) == 40) {
$producer_id[$j] = '30';
}
// in the above line if you leave the 30 without the ''
// array_unique won't work!
$i++;
}
$producer_ids = array_values(array_unique($producer_id));
?>
-------------------------------
Result is to have all producer-ID's in an array without dupes.
Following code copies unique values from MyArray to TempArray.
Then copies non-empty elements from TempArray to UniqueArray.
Not the most elegant solution, but it works.
<?php
$TempArray = array_unique($MyArray);
while (list($index,$data)=each($TempArray)) {
if (isempty($data)) {
$UniqueArray[$index]=$data;
}
}
?>
It seems that array_unique creates an exact copy of the original array and then elimitates duplicate values. It does NOT change the "internal references" of the array. For example:
<?php
$test_alfa = array();
$test_alfa[0] = "aa";
$test_alfa[1] = "aa";
$test_alfa[2] = "aa";
$test_alfa[3] = "bb";
$test_alfa[4] = "aa";
$test_alfa[5] = "bb";
$test_alfa[6] = "cc";
$test_alfa[7] = "bb";
$test_beta= array_unique($test_alfa);
$numValues = count($test_beta);
for ($i = 0 ; $i <= 7 ; $i++)
echo("test_beta[$i] = $test_beta[$i] <br>");
echo ("Number of elements in test_beta = $numValues ");
?>
will give you the following output:
test_beta[0] =
test_beta[1] = aa
test_beta[2] =
test_beta[3] =
test_beta[4] =
test_beta[5] = bb
test_beta[6] = cc
test_beta[7] =
Number of elements in test_beta = 3
The point is that you won't get the output you'd expect if you think that the values of the non duplicate elements are located in the first three array locations.
<?php
$numValues = count($test_beta);
for ($i=0;$i<=$numValues; $i++)
echo("test_beta[$i] = $test_beta[$i] <br>");
echo ("Number of elements in test_beta = $numValues ");
?>
will give you:
test_beta[0] =
test_beta[1] = aa
test_beta[2] =
Number of elements in test_beta = 3
Hope that saves u some debugging time!
Here's a little function I wrote that's similar to array_unique, for PHP3 users. It actually removes duplicated elements, but only works for 1 dimensional arrays. It also doesn't return a value, it changes the input array:
<?php
function array_unique(&$thearray)
{sort($thearray);
reset($thearray);
$newarray = array();
$i = 0;
$element = current($thearray);
for ($n=0;$n<sizeof($thearray);$n++)
{if (next($thearray) != $element)
{$newarray[$i] = $element;
$element = current($thearray);
$i++;
}
}
$thearray = $newarray;
}
?>