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.
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($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); ?>
In 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.
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); ?>
In 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.
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); ?>
In 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: "
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!