Current Location: Home> Function Categories> array_diff_uassoc

array_diff_uassoc

Use the user-provided callback function to perform index checking to calculate the difference set of the array
Name:array_diff_uassoc
Category:Array
Programming Language:php
One-line Description:Compare arrays, return the difference set (compare key names and key values, using user-defined key names comparison function).

array_diff_uassoc function

Applicable to PHP version

PHP 5.3.0 and above

Function description

This function is used to calculate the difference set of an array, returning elements that exist in the first array, but not in other arrays. It's similar to array_diff(), but it compares the key names of the array through a user-defined callback function.

Function Syntax

 <span class="fun">array_diff_uassoc(array $array1, array $array2, array ...$arrays, callable $key_compare_func): array</span>

parameter

  • array1 : The first array, the basic array used for comparison.
  • array2, ...arrays : One or more arrays that will be compared with the first array.
  • key_compare_func : A callback function used to compare array keys. The callback function receives two parameters, the keys of two arrays, and returns an integer: if the first key is less than, equal to or greater than the second key, it returns a negative, zero, or positive number.

Return value

Returns an array containing elements that appear in the first array but not in other arrays. The keys of the array remain as they are.

Example

 $array1 = ["a" => 1, "b" => 2, "c" => 3]; $array2 = ["a" => 1, "b" => 2]; $result = array_diff_uassoc($array1, $array2, function($key1, $key2) {<br>
return strcmp($key1, $key2);<br>
});</p>
<p>print_r($result);<br>

Description of sample code

In this example, $array1 and $array2 are both associative arrays. We compare their keys through a callback function. The callback function uses strcmp to compare key names and returns the result. If the first key is smaller than the second key, strcmp returns a negative value; if it is equal, return 0; if the first key is larger than the second key, return a positive value. Finally, array_diff_uassoc returns an array containing elements that exist only in the first array, and the keys of these elements are not in the second array.

Similar Functions
Popular Articles