Nick Shears
Nick Shears

Reputation: 1133

Check if any values in array are equal to each other

I'm validating a form that submits up to 3 different id's depending on what the user selects.

I've put them into an array:

$submitted_genres = array($_POST['genre1'], $_POST['genre2'], $_POST['genre3']);

How I can check to make sure that none of the array values are equal each other?

Upvotes: 3

Views: 870

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173542

You could use array_unique() to get an array of all unique values and then compare the size against the original array:

if (count(array_unique($submitted_genres)) !== count($submitted_genres)) {
    // there's at least one dupe
}

Upvotes: 5

Related Questions