假设我有一个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?
如果您想了解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)