Home > Backend Development > PHP Problem > How to compare multiple arrays in php to see if there are duplicate values

How to compare multiple arrays in php to see if there are duplicate values

青灯夜游
Release: 2023-03-16 16:36:01
Original
2010 people have browsed it

Comparison steps: 1. Use the array_intersect() function to compare multiple arrays and obtain the repeated values ​​​​(intersection elements) of the array. The syntax "array_intersect(array 1, array 2, array 3...)" will Return an intersection array; 2. Determine whether the intersection array is empty. The syntax is "intersection array ===[]". If it is empty, there are no duplicate values ​​in multiple arrays. If it is not empty, there are duplicate values ​​in multiple arrays.

How to compare multiple arrays in php to see if there are duplicate values

The operating environment of this tutorial: windows7 system, PHP8.1 version, DELL G3 computer

Compare multiple arrays and determine whether multiple arrays are If there are duplicate values, it means to determine whether multiple arrays have intersection elements.

Judgment steps:

Step 1: Use the array_intersect() function to compare multiple arrays and obtain the repeated values ​​(intersection elements) of the arrays

array_intersect() function is used to compare the values ​​of two (or more) arrays and return the intersection.

This function compares the values ​​of two (or more) arrays and returns an intersection array containing all values ​​in array1 that also appear in all other parameter arrays.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr1=array(0,1,2,3,4,5,6,7,8,9);
$arr2=array(0,2,4,6,8,10,12,14,16);
$arr3=array(0,2,4,8,16,32);
var_dump($arr1);
var_dump($arr2);
var_dump($arr3);
echo "多个数组的重复元素:";
$intersect=array_intersect($arr1,$arr2,$arr3);
var_dump($intersect);
?>
Copy after login

How to compare multiple arrays in php to see if there are duplicate values

Step 2: Determine whether the intersection array is empty

交集数组===[]
Copy after login
  • If it is empty, then more There are no duplicate values ​​in arrays

  • If they are not empty, there are duplicate values ​​in multiple arrays

Complete example implementation code :

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr1=array(0,1,2,3,4,5,6,7,8,9);
$arr2=array(0,2,4,6,8,10,12,14,16);
$arr3=array(0,2,4,8,16,32);
var_dump($arr1);
var_dump($arr2);
var_dump($arr3);
$intersect=array_intersect($arr1,$arr2,$arr3);

if($intersect===[]){
	echo "多个数组中没有重复元素";
}else{
	echo "多个数组中有重复元素";
}
?>
Copy after login

How to compare multiple arrays in php to see if there are duplicate values

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to compare multiple arrays in php to see if there are duplicate values. 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