检查数组中的任何值是否彼此相等

Nic*_*ars 3 php

我正在验证一个表单,根据用户选择的内容提交最多3个不同的ID.

我把它们放到一个数组中:

$submitted_genres = array($_POST['genre1'], $_POST['genre2'], $_POST['genre3']);
Run Code Online (Sandbox Code Playgroud)

我如何检查以确保没有任何数组值彼此相等?

Ja͢*_*͢ck 5

您可以使用array_unique()获取所有唯一值的数组,然后将大小与原始数组进行比较:

if (count(array_unique($submitted_genres)) !== count($submitted_genres)) {
    // there's at least one dupe
}
Run Code Online (Sandbox Code Playgroud)