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