phparraysarray-unique

PHP array unique does not work like expected


I have a problem with array_unique() in PHP. Here is my code:

$filterGroupsArray = ['', 'a', 'a', 'b'];
print_r($filterGroupsArray);
array_unique($filterGroupsArray);
print_r($filterGroupsArray);

The output is

Array ( [0] => [1] => a [2] => a [3] => b ) Array ( [0] => [1] => a [2] => a [3] => b )

but I am expecting

Array ( [0] => [1] => a [2] => a [3] => b ) Array ( [0] => [1] => a [2] => b )

What did I do wrong? Thank you very much!


Solution

  • array_unique returns a new array with the duplicate values removed.

    $uniqueArray = array_unique($filterGroupsArray);
    print_r($uniqueArray);