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)".
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");
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:
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....
$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:
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.
$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:
## Recommended learning: "
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
