Home  >  Article  >  Backend Development  >  Find the difference between two arrays in php

Find the difference between two arrays in php

WBOY
WBOYOriginal
2023-05-11 10:00:072115browse

In PHP programming, it is often necessary to perform operations on arrays, such as intersection, union, complement, etc. One of the commonly used operations is to find the difference between two arrays, that is, delete the elements present in one array in the other array to obtain the difference. In this article, we will introduce two different methods to find the difference of two arrays.

Method 1: Use the array_diff() function

The array_diff() function is a built-in function of PHP that can be used to compare the difference between two or more arrays and return a difference array. The form of this function is as follows:

array array_diff ( array $array1 , array $array2 [, array $... ] )

Among them, $array1 and $array2 are the two arrays to be compared, and $... indicates that multiple sets of array parameters can be passed for comparison. This function returns an associative array consisting of all elements present in $array1 but not present in any other parameter array.

Let’s take a look at an example:

$array1 = array("a", "b", "c", "d", "e");
$array2 = array("b", "d", "f");
$result = array_diff($array1, $array2);
print_r($result);

The output result of the above code is:

Array
(
    [0] => a
    [2] => c
    [4] => e
)

As you can see, the function successfully returns the difference between the two arrays. [a, c, e].

Method 2: Use foreach loop

In addition to using built-in functions, we can also use foreach loop to find the difference between two arrays. The implementation of this method is to traverse array 1 of the difference set to be found, and perform a circular comparison on array 2. If the element in array 1 exists in array 2, delete the element in array 2, otherwise it will be stored in array 2. into a result array.

Let’s take a look at a case:

$array1 = array("a", "b", "c", "d", "e");
$array2 = array("b", "d", "f");
$result = array();
foreach ($array1 as $value) {
    $exist = false;
    foreach ($array2 as $key => $val) {
        if ($val == $value) {
            unset($array2[$key]);
            $exist = true;
            break;
        }
    }
    if (!$exist) {
        $result[] = $value;
    }
}
print_r($result);

The result output by the above code is the same as method one:

Array
(
    [0] => a
    [2] => c
    [4] => e
)

As you can see, this method realizes the difference through double loops set of functions. In the first-level loop, each element in array 1 is traversed; in the second-level loop, each element in array 2 is traversed, and the same element is deleted through the unset function, and a flag bit $exist is used to record whether The same elements exist. Finally, elements not in array 2 are stored in the result array.

Summary

In PHP programming, finding the difference between two arrays is a common operation. This article introduces two methods of finding the difference set, one is to use the built-in function array_diff(), and the other is to implement it through a double loop. No matter which method is used, the difference set of arrays can be easily obtained.

The above is the detailed content of Find the difference between two arrays 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