How to compare two arrays in php and return different elements

青灯夜游
Release: 2023-03-16 07:16:01
Original
2282 people have browsed it

Comparison method: 1. Use the "array_diff(array1, array2)" statement to compare key values; 2. Use the "array_diff_key(array1, array2)" statement to compare key names; 3. Use The "array_diff_assoc(array 1, array 2)" statement simultaneously compares the key names and key values ​​of the array.

How to compare two arrays in php and return different elements

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

phpCompare two Array and return different elements

If you want to compare two arrays and obtain different elements, you need to compare the arrays and take the difference set.

In PHP, you can use the following function to get the difference set:

  • array_diff() function

  • ##array_diff_key() Function

  • array_diff_assoc() function

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

array_diff($arr1,$arr2...) 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, but do not exist. in other parameter array $arr2....

Example: Return different elements of the array

<?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);
$result=array_diff($arr1,$arr2);
echo "两个数组中,不同的元素为:";
var_dump($result);
?>
Copy after login

How to compare two arrays in php and return different elementsIn the above example, only one key name in the $arr1 array and the $arr2 array is different, Based on the array $arr1, the value "

"c"=>"blue"

" will be obtained.

2. array_diff_key() function - compares the key names of arrays

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

Example: Return different elements of the array

<?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);
$result=array_diff_key($arr1,$arr2);
echo "两个数组中,不同的元素为:";
var_dump($result);
?>
Copy after login

How to compare two arrays in php and return different elementsIn the above example, there are two key names in the $arr1 array and the $arr2 array that are different. If the array $arr1 is used, the values ​​"

"c"=>"blue"

" and ""d"=>"yellow"" will be obtained.

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

array_diff_assoc($arr1,$arr2...) function will compare the key name and key value of the array Both names and key values ​​are compared, and a difference array is also returned. The difference elements will be obtained from the compared array $arr1 like array_diff() and array_diff_key().

Example: Return different elements of the array

<?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);
$result=array_diff_assoc($arr1,$arr2);
echo "两个数组中,不同的元素为:";
var_dump($result);
?>
Copy after login

How to compare two arrays in php and return different elementsIn the above example, the $arr1 array and the $arr2 array are compared, and there are three different elements. Then based on the array $arr1, the values ​​​​"

"a"=>"red"

", ""c"=>"blue"", and "" will be obtained. "d"=>"yellow"". Recommended learning: "

PHP Video Tutorial

"

The above is the detailed content of How to compare two arrays in php and return different elements. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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