Home > Backend Development > PHP Problem > How to check if the values ​​in an array are not equal to each other in php

How to check if the values ​​in an array are not equal to each other in php

青灯夜游
Release: 2023-03-16 07:40:02
Original
2350 people have browsed it

Method: 1. Use "array_unique(array)" to remove duplicate values ​​in the original array, which will return a deduplicated array; 2. Use count() to obtain the length of the original array and the length of the deduplicated array. Syntax "count(array)"; 3. Compare whether the two lengths are equal. If they are equal, the values ​​in the array are not equal to each other. Otherwise, there are equal values.

How to check if the values ​​in an array are not equal to each other in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

php checks that the values ​​in the array are not mutually exclusive Equality is to check whether the elements in the array have duplicate values; if there are duplicate values, the array elements are not equal to each other.

So how to determine whether the elements in the array have duplicate values?

Implementation method:

  • Use array_unique() to remove duplicate values ​​in the original array and return the deduplicated array

  • Use count() to obtain the length of the original array and the length of the deduplication array respectively

  • Compare whether the length of the original array and the length of the deduplication array are equal

    • If they are equal, it means there are no duplicate values, that is, the values ​​in the original array are not equal to each other

    • If they are not equal, it means there are duplicate values, that is, the values ​​in the original array are not equal to each other. There are equal values ​​in the array.

Implementation example:

Judge whether the values ​​in the following arrays are not equal to each other

$arr1=array(2,3,5,32,1,2,4);
$arr2=array(3,4,5,6,7,8,9,1,2);
Copy after login

Code :

<?php
header("Content-type:text/html;charset=utf-8");
function f($arr){
	$len1=count($arr);
	$unique=array_unique($arr);
	$len2=count($unique);
	if($len1==$len2){
		echo "数组中值互不相等<br>";
	}else{
		echo "数组中有相等的值<br>";
	}
}
$arr1=array(2,3,5,32,1,2,4);
$arr2=array(3,4,5,6,7,8,9,1,2);
f($arr1);
f($arr2);
?>
Copy after login

How to check if the values ​​in an array are not equal to each other in php

It can be seen that there are equal values ​​in the $arr1 array, but the values ​​in the $arr2 array are not equal to each other.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to check if the values ​​in an array are not equal to each other in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template