Common array functions in php (1) (array_diff_assoc () array_diff () array_diff_key () array_diff_ukey () array_diff_uassoc (), arraydiffuassoc
Array_diff_assoc ($ arr1, $ arr2, $ arr3,... n );
Return: A $ arr1 copy, which appears in the subsequent ArraySame Key Value, Delete this element in the copy, and finally return this copy.
For example, $ arr1 contains elements ['k1 '=> 'v1'], $ arr2 ~ NThe same key name and key value'K1 '=> 'v1' does not return this element.
Array_diff (same as above)
Return: A $ arr1 copy, which appears in the subsequent ArraySame Value, Delete this element in the copy, and finally return this copy.
Array_diff_key (same as above)
Return: A $ arr1 copy, which appears in the subsequent ArraySame Key, Delete this element in the copy, and finally return this copy.
Array_diff_ukey (same as above, 'custom comparison function ')
The usage is similar to that of array_diff_key (), but the comparison rules can be set by yourself. The following sample code can be used to add some complicated ones. For example, it is an array of student data. If the name is repeated, the score is greater than 60.The two parameters of the custom function are key.
Array_diff_uassoc (same as above, 'custom comparison function ')
Similar to the previous one, except that the two parameters are elements (that is, 'key' => 'value ')
Below is. Demonstration of these five functions.
/*********** Array_diff_assoc (Statistics and key-Value Difference Sets of multiple arrays) ------ return a set of key values not included in the subsequent array of $ arr1 (array 2 ~ The same key value in array n does not contain this key value) * ***************/$ arr1 = array ('k1 '=> 'v1 ', 'k2' => 'v2', 'k3 '=> 'v3'); $ arr2 = array ('s1' => 'v1 ', 's2 '=> 'v2', 'k3' => 'v3 '); // The same as 'k3' => 'v3 'in $ arr1 ', the Return Value of the function does not include 'k3 '=> 'v3'. $ arr3 = array ('k1 '=> 'v1', 's2 '=> 'v2 '); // if there is the same 'k1 '=> 'v1', $ arrDiffAssoc = array_diff_assoc ($ arr1, $ arr2, $ arr3); var_dump ($ arrDiffAssoc);/*********** array_diff (Set of differences between statistics and values of multiple arrays )***** * **********/$ Arr1 = array ('k1 '=> 'v1', 'k2' => 'v2 ', 'k3 '=> 'v3'); $ arr2 = array ('a1' => 'v1 ', 'k2' => 'a2 '); $ arr3 = array ('a2 '=> 'v2'); $ arrDiff = array_diff ($ arr1, $ arr2, $ arr3); var_dump ($ arrDiff ); /*********** array_diff_key (Statistics and key difference sets of multiple arrays) * ***************/$ arr1 = array ('k1 '=> 'v1 ', 'k2' => 'v2', 'k3 '=> 'v3'); $ arr2 = array ('k1 '=> 'apple ', 'k2' => 'banana '); // compare only the key names. If the key-value pairs with the same key name are deducted from the return value of the function, $ arr3 = arra Y ('a1' => 'v1 '); $ arrDiffKey = array_diff_key ($ arr1, $ arr2, $ arr3); var_dump ($ arrDiffKey ); /*********** array_diff_ukey (use a custom callback function to calculate the key difference set from multiple arrays) * ***************/$ arr1 = array ('blue' => 1, 'red' => 2, 'green' => 3, 'purple '=> 4); $ arr2 = array ('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyany' => 8); $ arrDiffUkey = array_diff_ukey ($ arr1, $ arr2, 'Arr _ key_compare_func '); var_dump ($ arrDiffUkey ); function arr _ Key_compare_func ($ k1, $ k2) {if ($ k1 = $ k2) return 0; else if ($ k1> $ k2) return-1; else return 1 ;} /*********** array_diff_uassoc (use a custom callback function to calculate the key-value difference set from multiple arrays) * ***************/$ arr1 = array ('blue' => 1, 'red' => 2, 'green' => 3, 'purple '=> 4); $ arr2 = array ('green' => 5, 'blue' => 1, 'yellow' => 7, 'cyany' => 8); $ arrDiffUassoc = array_diff_uassoc ($ arr1, $ arr2, 'Arr _ assoc_compare_func'); var_dump ($ arrDi FfUassoc); function arr_assoc_compare_func ($ a, $ B) {if ($ a ===$ B) return 0; return ($ a> $ B )? 1:-1 ;}