0

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?

3
  • We need a lot more context; especially the structure of $linkphoto Commented Mar 21, 2009 at 15:17
  • solved the problem by creating a new array from the page displayed. It seems that the current array is a little bit crapy!! Commented Mar 21, 2009 at 18:18
  • It is a good habit to show a failing test when you claim that something is not working. For writing tests in Php you cah use PhpUnit (phpunit.de). Commented Mar 21, 2009 at 18:59

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

what shall i say i don't understand much PHP my array is the same as something like that array[1] = string array[2] = string i want to delete the duplicates if found
1

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.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.