3

我正在验证一个表单,该表单最多可提交 3 个不同的 ID,具体取决于用户选择的内容。

我已将它们放入一个数组中:

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

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

4

1 回答 1

5

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
}
于 2012-12-21T02:13:14.257 回答