How to compare two arrays in php and get different parts

青灯夜游
Release: 2023-03-09 19:20:02
Original
3700 people have browsed it

php method to compare two arrays and obtain different parts: 1. Use the array_diff() function, the syntax format "array_diff(array 1, array 2)"; 2. Use the array_diff_assoc() function, the syntax "array_diff_assoc (array 1, array 2)".

How to compare two arrays in php and get different parts

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

phpCompare two Array, method to get different parts:

Method 1: Use array_diff() function

array_diff() function returns the difference between two arrays set array. This array contains all keys that are in the array being compared, but are not in any of the other argument arrays.

In the returned array, the key names remain unchanged.

Grammar:

Grammar

array_diff(array1,array2,array3...)
Copy after login

Example:

"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("e"=>"red","f"=>"green","g"=>"blue"); $result=array_diff($a1,$a2); print_r($result); ?> // Array ( [d] => yellow )
Copy after login

Method 2: Use array_diff_assoc() function

## The #array_diff_assoc() function is used to compare the key names and key values of two (or more) arrays and return the difference.

This function compares the key names and key values of two (or more) arrays, and returns a difference array, which includes everything in the compared array (array1), but not in any other The key name and key value in the parameter array (array2 or array3, etc.).

Grammar:


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

Example:

"red","b"=>"green","c"=>"blue","d"=>"yellow"); $a2=array("a"=>"red","b"=>"green","c"=>"blue"); $result=array_diff_assoc($a1,$a2); print_r($result); // Array ( [d] => yellow )
Copy after login
Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to compare two arrays in php and get different parts. 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
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!