Home > Backend Development > PHP Problem > How to compare the differences between two array keys (values) in PHP

How to compare the differences between two array keys (values) in PHP

青灯夜游
Release: 2023-03-16 21:28:01
Original
3620 people have browsed it

In PHP, you can use the array_diff() function to compare the differences in the key values ​​(value) of two arrays; this function is used to compare the values ​​(value) of two (or more) arrays, and Returns a difference array containing different values, syntax "array_diff(array1,array2...);"; the difference array includes all values ​​in the compared array (array1) but not in any other parameter array (array2) .

How to compare the differences between two array keys (values) in PHP

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

Compare the difference between the two arrays, PHP provides Three methods:

  • array_diff()

  • array_diff_key()

  • array_diff_assoc()

Among them, if you want to compare only two array key values ​​(values) and obtain different elements, you need to use the array_diff() function.

array_diff() function - only compares key values ​​(value)

array_diff() function is used to compare two (or more) ) array values ​​and returns a difference array containing different values.

  • This function compares the values ​​​​of two (or more) arrays (value in key=>value) and returns a difference array, which includes all A value that is in the array being compared (array1), but not in any of the other argument arrays (array2 or array3, etc.).

array_diff(array1,array2);
Copy after login
ParametersDescription
array1Required. The first array to compare with other arrays.
array2Required. The array to compare to the first array.
  • Return value: Returns a difference array that includes everything in the compared array (array1), but not in any other parameters Values ​​in an array (array2 or array3, etc.).

Example: Compare the key values ​​(values) of two arrays and return the difference array

<?php  
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$arr2=array("e"=>"red","f"=>"green","g"=>"blue");
var_dump($arr1);
var_dump($arr2);
$result=array_diff($arr1,$arr2);
echo "两个数组的不同值:";
var_dump($result);
?>
Copy after login

How to compare the differences between two array keys (values) in PHP

Extended knowledge: two other comparison functions

1. array_diff_key(): only compare key names (key)

array_diff_key() function is used to compare the key names of two (or more) arrays and return the difference set.

array_diff_key(array1,array2...);
Copy after login

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$arr1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$arr2=array("a"=>"orange","b"=>"green","e"=>"red","r"=>"yellow");
var_dump($arr1);
var_dump($arr2);

echo "两个数组的不同值:";
$result=array_diff_key($arr1,$arr2);
var_dump($result);
?>
Copy after login

In the above example, there are two key names in the $arr1 array and the $arr2 array that are different. The value will be obtained based on the array $arr1. ##"c"=>"blue"" and ""d"=>"yellow"", so the output result is:

How to compare the differences between two array keys (values) in PHP

2. array_diff_assoc(): Compare key name (key) and key value (value)

array_diff_assoc() function is used to compare two (or more) The key name and key value of the array, and the difference is returned.

array_diff_assoc(array1,array2...);
Copy after login

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$arr1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$arr2=array("a"=>"orange","b"=>"green","e"=>"red","r"=>"yellow");
var_dump($arr1);
var_dump($arr2);

echo "两个数组的不同值:";
$result=array_diff_assoc($arr1,$arr2);
var_dump($result);
?>
Copy after login
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 compare the differences between two array keys (values) in PHP Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to compare the differences between two array keys (values) in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template