简体   繁体   English

如何检查foreach循环内的值是否相同

[英]How to check values inside foreach loop are same or not

i have a foreach loop as我有一个 foreach 循环

foreach ($age as $ages) {
   echo $ages;
}

when run this code the result is运行此代码时,结果是

3
3

or或者

1
3

i want to check if values are same or not.我想检查值是否相同。 For example例如

if (values are same) {
  do something...
}
else {
  do something...
}

i really couldn't find how to check values are same or not.我真的找不到如何检查值是否相同。

If you are checking to see if all values are identical just use array_unique() to remove duplicate values.如果您要检查所有值是否相同,只需使用array_unique()删除重复值。 Then check to see if the size of the remaining array is equal to 1:然后检查剩余数组的大小是否等于1:

$is_all_duplicates = count(array_unique($array)) === 1;

If any duplicates will trigger this condition, just use array_unique() to remove duplicate values and then compare the sizes of the two arrays.如果有任何重复将触发此条件,只需使用array_unique()删除重复值,然后比较两个 arrays 的大小。 If they are not the same you had duplicate values:如果它们不相同,则您有重复的值:

$is_some_duplicates = count(array_unique($array)) < count($array);

If values are the same, then array_count_values will have one element only:如果值相同,则array_count_values将只有一个元素:

$a = [1,2,3,3];
$acv = array_count_values($a);
if (count($acv) == 1) {
    echo 'values are the same';
} else {
    echo 'values are NOT the same';
}


$a = [3,3,3];
$acv = array_count_values($a);
if (count($acv) == 1) {
    echo 'values are the same';
} else {
    echo 'values are NOT the same';
}

The fiddle .小提琴

But the most optimal solution is a simple loop with break on first value that is not equal to first of the array:但最优化的解决方案是一个简单的循环,第一个值不等于break的第一个值:

$a = [1,2,3,3];
$hasSameValues = true;

$firstElem = array_slice($a, 0, 1)[0];
foreach ($a as $el) {
    if ($el != $firstElem) {
        $hasSameValues = false;
        break;
    }
}

if ($hasSameValues) {
    echo 'values are the same';
} else {
    echo 'values are NOT the same';
}

echo PHP_EOL;

$a = [3,3,3];
$hasSameValues = true;

$firstElem = array_slice($a, 0, 1)[0];
foreach ($a as $el) {
    if ($el != $firstElem) {
        $hasSameValues = false;
        break;
    }
}

if ($hasSameValues) {
    echo 'values are the same';
} else {
    echo 'values are NOT the same';
}

Yet another fiddle .又一个小提琴

You possible want to 'say':)你可能想“说”:)

foreach ($ages as $age) {
   echo $age;
}

I understand that you want to check if only last value is equal with actual value.我了解您想检查是否只有最后一个值与实际值相等。
To check that write this:要检查,写这个:

$ages = [1,1,7,2,2,4,1,2];

foreach ($ages as $age) {
    if(isset($temp) ? !($temp == $age)  : true){
       echo $age;
    }
    $temp = $age;
}

The result is: 172412结果是:172412
The code:编码:
Check if variable $temp is set: isset($temp)检查是否设置了变量$tempisset($temp)
If it is set, check if it is NOT equal with last age value: !($temp == $age)如果已设置,请检查它是否与最后一个年龄值不相等: !($temp == $age)
If the $temp isn't set, set true to run the code from if statement (echo the age)(is there for first run).如果$temp未设置,则设置true以从 if 语句运行代码(回显年龄)(首次运行时是否存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM