Current Location: Home> Function Categories> array_diff_ukey

array_diff_ukey

Use callback function to compare key names to calculate the difference set of arrays
Name:array_diff_ukey
Category:Array
Programming Language:php
One-line Description:Compare arrays and return the difference set (compare only key names, use user-defined key names comparison function).

Definition and usage

The array_diff_ukey() function is used to compare the key names of two (or more) arrays and return the difference set.

Note: This function uses a user-defined function to compare key names!

The function compares the key names of two (or more) arrays and returns an array of differences that include all key names in the array being compared ( array1 ) but not in any other parameter array ( array2 or array3 , etc.).

Example

Example 1

Compare the key names of two arrays (using user-defined functions to compare key names) and return the difference:

 <?php
function myfunction ( $a , $b )
{
if ( $a === $b )
  {
  return 0 ;
  }
  return ( $a > $b ) ? 1 : - 1 ;
}

$a1 = array ( "a" => "red" , "b" => "green" , "c" => "blue" ) ;
$a2 = array ( "a" => "blue" , ​​"b" => "black" , "e" => "blue" ) ;

$result = array_diff_ukey ( $a1 , $a2 , "myfunction" ) ;
print_r ( $result ) ;
?>

Try it yourself

Example 2

Compare the key names of three arrays (using user-defined functions to compare key names) and return the difference:

 <?php
function myfunction ( $a , $b )
{
if ( $a === $b )
  {
  return 0 ;
  }
  return ( $a > $b ) ? 1 : - 1 ;
}

$a1 = array ( "a" => "red" , "b" => "green" , "c" => "blue" ) ;
$a2 = array ( "a" => "black" , "b" => "yellow" , "d" => "brown" ) ;
$a3 = array ( "e" => "purple" , "f" => "white" , "a" => "gold" ) ;

$result = array_diff_ukey ( $a1 , $a2 , $a3 , "myfunction" ) ;
print_r ( $result ) ;
?>

Try it yourself

grammar

 array_diff_ukey ( array1 , array2 , array3 ... , myfunction ) ;
parameter describe
array1 Required. The first array that is compared with other arrays.
array2 Required. The array that compares to the first array.
array3 ,... Optional. Other arrays that are compared with the first array.
myfunction Required. Defines a string that calls the comparison function. If the first parameter is less than, equal to or greater than the second parameter, the comparison function must return an integer less than, equal to or greater than 0.

illustrate

array_diff_ukey() returns an array that includes all the values ​​of key names that appear in array1 but not in any other parameter array. Note that the relationship remains unchanged. Unlike array_diff() , comparisons are based on key names rather than values.

This comparison is done through a user-provided callback function. If the first parameter is considered to be smaller than, equal to, or greater than the second parameter, it must return an integer less than, equal to, or greater than, respectively.