Home  >  Article  >  Backend Development  >  How to determine whether two arrays are duplicated in php

How to determine whether two arrays are duplicated in php

PHPz
PHPzOriginal
2023-04-19 10:09:39955browse

In PHP development, it is common to determine whether two arrays have duplicates. This article will introduce two methods to achieve this requirement.

Method 1: Use the array_intersect function

Among the array functions officially provided by PHP, there is a function called array_intersect, which can be used to compare two arrays to see if they have duplicate elements.

The sample code is as follows:

$array1 = array(1, 2, 3, 4, 5);
$array2 = array(4, 5, 6, 7, 8);

$result = array_intersect($array1, $array2);

if(count($result) > 0) {
    echo "存在重复元素";
} else {
    echo "不存在重复元素";
}

Analysis:

  1. First, define two arrays $array1 and $array2, each containing some numbers;
  2. Then, use the array_intersect function to compare the two arrays;
  3. If the number of elements in the returned array is greater than 0, it means that the two arrays have duplicate elements;
  4. Otherwise, it means that there are two The array has no duplicate elements.

It should be noted that when using the array_intersect function, you need to ensure that the element types in the two arrays are consistent. If the elements in one array are strings and the elements in the other array are numbers, the comparison will also fail.

Method 2: Use loop comparison

If you don’t want to introduce a new function to determine whether two arrays have duplicate elements, you can also use a loop to achieve this function.

The sample code is as follows:

$array1 = array(1, 2, 3, 4, 5);
$array2 = array(4, 5, 6, 7, 8);

$hasRepeat = false;

foreach ($array1 as $value1) {
    foreach ($array2 as $value2) {
        if ($value1 == $value2) {
            $hasRepeat = true;
            break;
        }
    }
}

if ($hasRepeat) {
    echo "存在重复元素";
} else {
    echo "不存在重复元素";
}

Analysis:

  1. First, define two arrays $array1 and $array2, each containing some numbers;
  2. Then, use two foreach loops to traverse the two arrays;
  3. In the loop, use the if statement to compare whether the current two elements are equal;
  4. If they are equal, then the two If two arrays have duplicate elements, set the $hasRepeat variable to true and jump out of the loop;
  5. If the $hasRepeat variable is still false at the end of the loop, it means that the two arrays have no duplicate elements.

It should be noted that when using loop comparison, the time complexity will be relatively high because two levels of loops need to be nested. If the number of elements in both arrays is large, performance may be affected.

Summary

Determining whether two arrays have duplicate elements is a common requirement in PHP development. This article introduces two implementation methods, one is to use the array_intersect function, and the other is to use loop comparison. Different methods need to be chosen depending on the specific situation.

The above is the detailed content of How to determine whether two arrays are duplicated in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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