检查php数组中是否存在两个不同的值

use*_*368 0 php arrays

假设我有一个php数组,可以是全1,全2或全1和2.例如,我可以拥有array(1, 1, 1, 1, 1, 1),array(2, 2, 2, 2, 2, 2)或者array(2, 2, 1, 1, 2, 1).

如何检查我的数组实际上是否是一个全1的数组,全部为2,或者我的数组实际上是否同时包含1和2?

mar*_*rio 5

如果您想了解PHP,可以使用它array_unique()来探测存在哪些不同的值:

 if (count(array_unique($array)) == 1) {
     // It's either full of 1s or 0s.
     // Then just probe the first entry.
 }
Run Code Online (Sandbox Code Playgroud)