Compare multiple values ​​with each other

I have about 20 different variables and I want to compare these variables with each other to check how equal or not they are.

Example

$var1 = 1; $var2 = 2; $var3 = 1; $var4 = 8; . . . $var10 = 2; 

Now I want to check ...

 if($var1 == $var2 || $var1 == $var3 || $var1 == $var4 || ......... || $var2 == $var3 || $var2 == $var4 || ............. || $var8 = $var9 || $var8 == $var10 ||...) { echo 'At-least two variables have same value'; } 

I find it easy. Any suggestions?

+6
source share
3 answers
 $arr = array($var1, $var2, ... , $var10); if (count($arr) !== count(array_unique($arr))) { echo 'At-least two variables have same value'; } 
+10
source

If you want to find out if any of the variables are duplicates, put them in an array and use array_count_values :

array_count_values() returns an array using the values ​​of the input array as keys and their frequency in the input as values.

If you have values ​​greater than 1 as a result, there is a match.

eg.

 $values = array(1,2,3,1); if(max(array_count_values($values)) > 1) { ... 
+7
source

first, save them in an array and it will get easier

 $list=array("1"=>$var1,"2"=>$var2,......,"10"=>$var10); $list2=array_unique($list); if(count($list2) != count($list)) echo 'At-least two variables have same value'; 
0
source

Source: https://habr.com/ru/post/922696/


All Articles