1

I have an array in variable $votes. print_r($votes) gives us:

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

So we have three values, all of which are set to 1.

Now, I want to make the array only have unique values, meaning that if there are three values that match 1, then remove two of them.

To achieve this, I tried array_unique($votes); but it did not remove any values. Why?!

0

1 Answer 1

3

You have to assign the output of array_unique to the array again like this:

$votes = array_unique($votes);

As a reference you can look at the manual: http://php.net/manual/en/function.array-unique.php

And a quote from there:

Takes an input array and returns a new array without duplicate values.

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

5 Comments

Kindly point a gun right to my head. Thanks. I just realised this mistake :)
Question, how did you realise I made this mistake? I can't see how you noticed this via my question. Good catch nonetheless.
@HenrikPetterson Because you wrote: I tried array_unique($votes); So without the assignment
In that case, just approve the answer.
@Sylverdrag There is a 15 min delay before I can mark it as fixed :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.