Home >>PHP Array Functions >PHP array_diff_ukey() Function

PHP array_diff_ukey() Function

PHP array_diff_ukey() Function

PHP array_diff_ukey() Function is used to compare the keys of two or more given arrays using a user-defined function. It accepts at-least three parameters and all the three parameters are mandatory and the others are optional.It returns an array containing the entries from array1 that are not present in other given arrays.

Syntax:

array_diff_ukey($array1, $array2, $array3, ..., function);

Parameter Values

Parameter Description
array1 This is a requiredparameter.This parameter definesthe array to compare from.
array2 This is a requiredparameter.This parameter definesan array to compare against.
array3,... This is an optionalparameter.This parameter definesmore arrays to compare against.
function This is a requiredparameter.This parameter definesa string that define a callable comparison function.

Here is an example of array_diff_ukey() function in PHP:

<html>
<body>
<pre>
<?php
function compare($x,$y)
{
if ($x===$y)
  {
  return 0;
  }
  return ($x>$y)?1:-1;
}
$x=array("a"=>"1","b"=>"2","c"=>"3","d"=>"4");
$y=array("d"=>"1","b"=>"2","e"=>"3","d"=>"3");

$result=array_diff_ukey($x,$y,"compare");  // compares only keys
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [a] => 1
    [c] => 3
)

Example 2:

<html>
<body>
<pre>
<?php
function compare($x,$y)
{
if ($x===$y)
  {
  return 0;
  }
  return ($x>$y)?1:-1;
}
$x=array("name"=>"Jerry","age"=>"21","place"=>"Noida","project"=>"WebApp");
$y=array("name"=>"Jerry","age"=>"19","place"=>"Noida","company"=>"Google");

$result=array_diff_ukey($x,$y,"compare");  // compares only keys.
print_r($result);
?>
</pre>
</body>
</html>
Output:
Array
(
    [project] => WebApp
)

No Sidebar ads