Home  >  Article  >  Backend Development  >  php finds different array elements

php finds different array elements

王林
王林Original
2023-05-06 14:07:07528browse

In PHP, sometimes you need to find the different elements between two arrays. There may be multiple solutions to this problem, and here are a few common ones.

Method 1: Use the array_diff function

There is a built-in function array_diff in PHP that can be used to compare different elements between two arrays.

The syntax of this function is as follows:

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

Among them, $array1 is the first array, $array2 is the second array, [, array $...] is the optional first array Three, fourth,... array.

This function returns an array containing all elements that appear in the first array but are not in subsequent arrays.

The following is an example of using the array_diff function to find different elements:

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

The output result of the above code is:

Array
(
    [3] => d
    [4] => e
)

Method 2: Use foreach loop to traverse

Another method is to use a foreach loop to traverse two arrays and compare each element of them. This requires using a counter to mark the number of distinct elements and storing them in a new array.

The following is an example of using a foreach loop to traverse to find different elements:

$array1 = array("a", "b", "c", "d", "e");
$array2 = array("a", "b", "c", "f", "g");
$diff = array();
$count = 0;

foreach($array1 as $val){
    if(!in_array($val, $array2)){
        $diff[$count] = $val;
        $count++;
    }
}

foreach($array2 as $val){
    if(!in_array($val, $array1){
        $diff[$count] = $val;
        $count++;
    }
}

print_r($diff);

The output result of the above code is:

Array
(
    [0] => d
    [1] => e
    [2] => f
    [3] => g
)

Method 3: Use the array_merge function and array_unique function

This method is to merge two arrays into one array, and then use the array_unique function to find different elements.

The following is an example of using the array_merge function and array_unique function to find different elements:

$array1 = array("a", "b", "c", "d", "e");
$array2 = array("a", "b", "c", "f", "g");
$union = array_merge($array1, $array2);
$diff = array_unique($union);

print_r($diff);

The output of the above code is:

Array
(
    [3] => d
    [4] => e
    [5] => f
    [6] => g
)

Summary

The above is Three ways to find different elements. Among them, the array_diff function is the simplest and easiest to use method. You only need to pass two arrays to it to get different elements. Although using foreach loop traversal requires writing more code, the execution speed of the code will be faster than the array_diff function. Finally, although the code of using the array_merge function and array_unique function is simple, it requires creating a new array in memory to store data, which may cause some performance problems. Therefore, you need to consider your choice when using it.

The above is the detailed content of php finds different array elements. 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
Previous article:php exists in the arrayNext article:php exists in the array