PHP array learning: How to compare two arrays and find the difference set

青灯夜游
Release: 2023-03-11 21:38:01
Original
4595 people have browsed it

In the previous article, we introduced the method of comparing arrays to obtain the same elements (intersection). If you are interested, you can click to read → "PHP Array Learning: How to Compare Two Arrays to Find the Intersection". Since the intersection can be found, the difference set can also be found. How to obtain the difference set? This article will introduce to you several ways to compare arrays in PHP to obtain different elements (difference sets).

Same as the previous article, let’s first take a look at the function that compares two arrays and finds the difference. There are multiple functions built into PHP. This article mainly introduces three commonly used functions: array_diff(), array_diff_key(), array_diff_assoc(), they will compare arrays in terms of "key value", "key name", "key value and key name" respectively, and return a difference array. The difference elements will be from the compared array ( first parameter).

It’s hard to understand in vain. Let’s use code examples to see how array_diff(), array_diff_key() and array_diff_assoc() compare arrays and find differences.

We have the following two arrays:

$arr1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$arr2=array("a"=>"orange","b"=>"green","e"=>"red","r"=>"yellow");
Copy after login

1. Use the array_diff() function--compare the key values ​​of the array

Copy after login

array_diff($arr1,$arr2...)The function only compares the key values ​​of the array and returns a difference array. The elements in the difference array exist in the compared array$arr1 in, but does not exist in other parameter arrays $arr2....

Let’s take a look at the above example. When comparing the $arr1 array with the $arr2 array, the key values ​​that only exist in the $arr1 array are: “ blue", so the output result is:

PHP array learning: How to compare two arrays and find the difference set

2. Use the array_diff_key() function--compare the key names of the array

Copy after login

array_diff_key($arr1,$arr2...)The function only compares the key names of the arrays, and also returns a difference array. The elements in the difference array exist in the compared array# in ##$arr1, but does not exist in other parameter arrays $arr2....

In the above example, the two key names in the

$arr1 array and the $arr2 array are different, with the array $arr1 The values ​​""c"=>"blue"" and ""d"=>"yellow"" will be obtained, so the output result is:

PHP array learning: How to compare two arrays and find the difference set

3. Use the array_diff_assoc() function - compare the key name and key value of the array

Copy after login

array_diff_assoc($arr1, $arr2...)The function will compare the key name and key value of the array, and also return a difference array. The difference set elements will be from the compared array like array_diff() and array_diff_key() Obtained from $arr1.

In the above example, the

$arr1 array and the $arr2 array are compared. There are three different elements, and then the value will be obtained based on the array $arr1." "a"=>"red"", ""c"=>"blue"", ""d"=>"yellow"", so the output result is:

PHP array learning: How to compare two arrays and find the difference set

Okay, that’s all. If you want to know anything else, you can click this. → →

php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays:

PHP function array array function video explanation, come and learn!

The above is the detailed content of PHP array learning: How to compare two arrays and find the difference set. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!