Open In App

PHP array_diff_ukey() Function

Last Updated : 20 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The array_diff_ukey() function is an inbuilt function in PHP. It is used to compares the key two or more arrays using a user-defined function, and return an array, which is array1 and its not present any others array2, array3 or more… Syntax:

array_diff_ukey($array1, $array2, $array3..., arr_diffukeyFunction)

Parameters Used: This function accepts minimum three parameters and all three parameter is mandatory and the other is optional. The parameters are described below:

  1. $array1(mandatory): The array will be compared with other arrays.(such that array compare from array1).
  2. $array2(mandatory): The array Compared with the first array.
  3. $array3(Optional): The array Compared with the first array.
  4. arr_diffukeyFunction(mandatory): It is Required user-defined function. A string that defines a callable comparison function. The comparison function returns an integer than 0 if the first argument is than the second argument.

Return Value: Returns an array containing the entries from array1 that are not present in other arrays such as:-(arra2, arra3, arar4….more). If all values are present in other arrays the function return NULL. The return value type is an array. Example 1:

Input: 
$arr1 = array("one"=>"C Program", "two"=>"PHP Program", "three"=>"Java Program ");
$arr2 = array("one"=>"Java Program", "two"=>"C++ Program", "six"=>"Java Program");

Output: 
Array
(
    [three] => Java Program 
)

Explanation: First two values of arr1 are matched with arr2 and last value 
             not matched so the function returns last value.

Example 2:

Input: 
$arr1=array("one"=>"C Program", "two"=>"PHP Program", "three"=>"Java Program ");
$arr2=array("one"=>"Java Program", "two"=>"C++ Program", "three"=>"Java Program");

Output: 
Array
(
)

Explanation: All values are matched with arr2 so function return null values.

Let’s take an example for understand the array_diff_ukey() Function.

  • Program 1 : Taking two array (array1 and array2) and using user-defined key comparison function (diffukeyFunction). Simple solution in PHP language : 

PHP




<?php
  
// Program of array_diff_ukey function in PHP
  
function arr_diffukeyFunction($one, $two)
{
    if ($one === $two) {
        return 0;
    }
    return ($one > $two) ? 1 : -1;
}
  
// Driver Code 
  
$arr1 = array(
    "one" => "C Program",
    "two" => "PHP Program",
    "three" => "Java Program "
);
$arr2 = array(
    "one" => "Java Program",
    "two" => "C++ Program",
    "six" => "Java Program"
);
  
$result = array_diff_ukey($arr1, $arr2, "arr_diffukeyFunction");
print_r($result);
?>


Output:

Array
(
    [three] => Java Program 
)
  • Program 2 : Take three array (array1, array2 and arra3) and using user-defined key comparison function (diffukeyFunction). 

PHP




<?php
  
// Program of array_diff_ukey function in PHP
  
function arr_diffukeyFunction($one, $two)
{
    if ($one === $two) {
        return 0;
    }
    return ($one > $two) ? 1 : -1;
}
  
// Driver Code 
  
$arr1 = array(
    "one" => "C Program",
    "two" => "PHP Program",
    "three" => "Java Program "
);
$arr2 = array(
    "one" => "XML Program",
    "two" => "C++ Program",
    "four" => "CSS Program"
);
$arr3 = array(
    "five" => "MVC Program",
    "six" => "C# Program",
    "one" => "ASP .NET Program"
);
  
$result = array_diff_ukey($arr1, $arr2, $arr3, "arr_diffukeyFunction");
print_r($result);
?>


Output:

Array
(
    [three] => Java Program 
)
  • Program 3 : Return Null if all values are matched in other arrays 

php




<?php
  
// Program of array_diff_ukey function in PHP
function arr_diffukeyFunction($one, $two)
{
    if ($one === $two) {
        return 0;
    }
    return ($one > $two) ? 1 : -1;
}
  
// Driver Code 
$arr1 = array(
    "one" => "C Program",
    "two" => "PHP Program"
);
$arr2 = array(
    "one" => "Java Program",
    "two" => "C++ Program"
);
  
$result = array_diff_ukey($arr1, $arr2, "arr_diffukeyFunction");
print_r($result);
?>


  • Output:
Array
(
)
  • Program 4 : If we take only one array (array1) and using user-defined key comparison function (diffukeyFunction)There are no output and it gives “RUNTIME ERROR” warning message. 

PHP




<?php
  
//Program of array_diff_ukey function in PHP
  
function arr_diffukeyFunction($one, $two)
{
    if ($one === $two) {
        return 0;
    }
    return ($one > $two) ? 1 : -1;
}
  
// Driver Code 
  
$arr1 = array(
    "one" => "C Program",
    "two" => "PHP Program",
    "three" => "Java Program "
);
  
//take only one array
  
$result = array_diff_ukey($arr1, "arr_diffukeyFunction");
print_r($result);
?>


  • Output:
No Output
  • Warning:
PHP Warning:  array_diff_ukey(): at least 3 parameters are required,
2 given in /home/c0177af9f69e897ad93cc9855a9ae415.php on line 23


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads