3
我正在驗證提交最多3個不同ID的表單,具體取決於用戶選擇的內容。檢查數組中的任何值是否相等
我已經把它們放進一個數組:
$submitted_genres = array($_POST['genre1'], $_POST['genre2'], $_POST['genre3']);
如何我可以檢查,以確保沒有一個數組值相等對方?
我正在驗證提交最多3個不同ID的表單,具體取決於用戶選擇的內容。檢查數組中的任何值是否相等
我已經把它們放進一個數組:
$submitted_genres = array($_POST['genre1'], $_POST['genre2'], $_POST['genre3']);
如何我可以檢查,以確保沒有一個數組值相等對方?
你可以使用array_unique()
來獲取所有唯一值的數組,然後對原來的數組比較大小:
if (count(array_unique($submitted_genres)) !== count($submitted_genres)) {
// there's at least one dupe
}
非常好,謝謝! –