tags:

views:

364

answers:

2

I have an array in PHP and I want to remove duplicates.

I simply turned to the function array_unique() to create a new array and remove the duplicates.

Here's the code:

$unlink = array();
$unlink = array_unique($linkphoto);

foreach ($unlink as $link) {
    echo $link, "<br />";
}

Still it shows duplicates! Any suggestions about what's wrong?

+2  A: 

According to the documentation, the condition for equality is as follows:

Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used.

What sort of data are you using? If two items aren't string equal, then they'll both remain in the array.

Kyle Cronin
what shall i say i don't understand much PHPmy array is the same as something like thatarray[1] = stringarray[2] = stringi want to delete the duplicates if found
Omar Abid
+1  A: 

We need more context in order to be able to help you, like what the contents are of $linkphoto before array_unique is applied to it. For example:

<?php
$array = Array('A','B','C','D','B');
print_r($array); // Array ( [0] => A [1] => B [2] => C [3] => D [4] => B )
$result = array_unique($array);
print_r($result); // Array ( [0] => A [1] => B [2] => C [3] => D ) 
?>

As @nobody_ mentioned, it will only eliminate duplicates if their string representations are the same.

Paolo Bergantino
normaly it should contain elements like the array above, but it's not currently acting like an array.Anyway I found a solution by creating a new array and filling it with the $linkphoto
Omar Abid