Home  >  Article  >  Backend Development  >  php different items of 2 arrays

php different items of 2 arrays

WBOY
WBOYOriginal
2023-05-05 20:55:06542browse

PHP is a very popular programming language, and we often encounter various problems that require arrays as data types. One of the common questions is how to find the different items in two arrays. This article will introduce three common methods to solve this problem.

Method 1: Use the array_diff() function

PHP provides a very convenient function for comparing different items of two arrays. This function is array_diff().

For example, we have two arrays, $arr1 and $arr2:

$arr1 = array('apple', 'banana', 'orange', 'pear');
$arr2 = array('apple', 'banana', 'kiwi', 'grape');

We want to find out the different items in the two arrays, then we can use the array_diff() function As follows:

$diff = array_diff($arr1, $arr2);
print_r($diff);

The output result is:

Array
(
    [2] => orange
    [3] => pear
)

Method 2: Use the array_diff_key() function

If we want to compare the keys of the array instead of the values, we can use array_diff_key() function to solve the problem.

For example, we have two arrays, $arr1 and $arr2, where the keys of $arr1 are 'a', 'b', 'c', and the keys of $arr2 are 'a', 'd', 'e', ​​we want to find out the different keys in the two arrays, then we can use the array_diff_key() function as follows:

$arr1 = array('a'=>1, 'b'=>2, 'c'=>3);
$arr2 = array('a'=>1, 'd'=>4, 'e'=>5);
$diff = array_diff_key($arr1, $arr2);
print_r($diff);

The output result is:

Array
(
    [b] => 2
    [c] => 3
)

Method Three: Use foreach loop

In addition to using the predefined functions provided by PHP, we can also use foreach loop to compare different items of two arrays.

For example, we have two arrays, $arr1 and $arr2:

$arr1 = array('apple', 'banana', 'orange', 'pear');
$arr2 = array('apple', 'banana', 'kiwi', 'grape');

We want to find out the different items in the two arrays, then we can use a foreach loop to achieve it As follows:

$diff = array();
foreach($arr1 as $val) {
    if (!in_array($val, $arr2)) {
        $diff[] = $val;
    }
}
foreach($arr2 as $val) {
    if (!in_array($val, $arr1)) {
        $diff[] = $val;
    }
}
print_r($diff);

The output result is:

Array
(
    [2] => orange
    [3] => pear
    [4] => kiwi
    [5] => grape
)

Summary

The above are three commonly used methods to find different items in two arrays. Each method has its own Their respective characteristics and applicable scenarios need to be selected and used according to the actual situation. No matter which method is used, when performing array comparison, you should pay attention to factors such as the type and quantity of the keys and values ​​of the array to ensure the accuracy of the comparison results.

The above is the detailed content of php different items of 2 arrays. 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