Home  >  Article  >  Backend Development  >  How to get different elements of two arrays in php

How to get different elements of two arrays in php

PHPz
PHPzOriginal
2023-04-18 10:20:20970browse

In PHP, sometimes we need to get the different items between two arrays, that is to say, find the elements that exist in array A but not in array B, or that exist in array B but not in array A. elements of existence.

This kind of operation is very common in actual development. For example, in terms of difference comparison, data synchronization, etc., we may need to find the difference between two arrays and handle it accordingly.

So, in PHP, how to get the different items between two arrays? Below I will introduce two methods in detail to you.

Method 1: Use the array_diff() function

PHP has a built-in very convenient function array_diff(), which can calculate the difference and return the different items between array A and other arrays .

The specific usage is as follows:

$a = array('apple', 'banana', 'orange', 'watermelon');
$b = array('apple', 'banana', 'grape');
$diff = array_diff($a, $b);
print_r($diff);

The output result is:

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

In the above code, we first define two arrays $a and $b, which contain different element. Then, we called the array_diff() function, passing in two arrays as parameters. The function will automatically calculate the difference and save the result in the variable $diff. Finally, we use the print_r() function to output the contents of the $diff variable to view the different items between the two arrays.

It should be noted that the array_diff() function can only find elements that exist in array A but not in other arrays, but cannot find elements that exist in array B but do not exist in array A. If we need to find different items in array B, we need to use the array_diff() function to execute it again. The code is as follows:

$diff = array_diff($b, $a);
print_r($diff);

The output result is:

Array
(
    [2] => grape
)

In the above code, we called array_diff () function twice, passing in two arrays respectively, to obtain the difference between the two arrays more intuitively.

Method 2: Use the array_unique() function

In addition to the array_diff() function, another way to obtain different items is to use the array_unique() function, which can combine repeated elements in the array Remove and return the distinct items within it.

The specific usage is as follows:

$a = array('apple', 'banana', 'orange', 'watermelon');
$b = array('apple', 'banana', 'grape');
$diff = array_unique(array_merge($a, $b));
print_r($diff);

The output result is:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => watermelon
    [4] => grape
)

In the above code, we first use the array_merge() function to merge the two arrays into a new array, and then Then pass the new array into the array_unique() function, which will automatically remove duplicate items and retain only different items.

It should be noted that when using this method to obtain different items, we need to merge all the arrays that need to be compared into a new array, so when processing a large amount of data, there may be a certain burden on the memory. If you have projects with high requirements on performance and memory usage, it is recommended to use the array_diff() method.

To sum up, we have introduced two methods to get different items in PHP: array_diff() and array_unique(). These methods are very simple and easy to use, can quickly complete the difference comparison operation, and play an important role in actual development.

The above is the detailed content of How to get different elements of 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