Home  >  Article  >  Backend Development  >  How to find the difference set of arrays in php

How to find the difference set of arrays in php

青灯夜游
青灯夜游Original
2022-06-29 17:17:5810531browse

3 methods: 1. Use array_diff() to compare the key values ​​of the array and return the difference set in the form of an array. The syntax is "array_diff(array 1, array 2)"; 2. Use array_diff_key() to compare the arrays. The key name and return the difference in the form of an array, the syntax is "array_diff_key(array 1, array 2)"; 3. Use array_diff_assoc() to compare the array key name and key value, the syntax is "array_diff_assoc(array 1, array 2)".

How to find the difference set of arrays in php

The operating environment of this tutorial: windows7 system, PHP version 8.1, DELL G3 computer

In PHP, you can use the following 3 built-in Function to find the array difference:

  • 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 the difference set and difference set elements in the form of an array Will be obtained from the array being compared (the first parameter).

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");

How to find the difference set of arrays in php

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

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 in array $arr1, 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:

How to find the difference set of arrays in php

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

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:

How to find the difference set of arrays in php

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

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:

How to find the difference set of arrays in php## Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to find the difference set of 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