PHP array_diff_uassoc() Function

What is PHP array_diff_uassoc() Function?

If you want to know the elements (in respect to both keys and values) that exist in one array and those don’t exist in other arrays (one or more arrays), use array_diff_uassoc() function. A function uses a user defined key comparison function to compare the keys.

Syntax:

array_diff(array1, array2, array3, …, key_comparison_function)

Parameters:

The Function has 3 required parameters and 1 optional parameter-

array1 (Required): This is the first array whose elements (both keys and values) the function checks whether exist in other arrays or not.

array2 (Required): This is another array whose elements (both keys and values) the function compares with the first array. Check example 1.

array3, … (Optional): This is one or more arrays whose elements (both keys and values) the function compares with the first array. Check example 2.

key_compare_function (Required): This is the callback function that determines the comparison of the two arrays. The function must return the followings-

  • 0 if both arguments are equal.
  • Greater than 0 if first argument is greater than the second one.
  • Less than 0 if the first argument is less than the second one.

Return Values:

The function returns an array.

Examples:

Example 1:

<pre>
<?php
function key_compare_function1($a,$b){
    if ($a === $b){
      return 0;
      }
      return ($a>$b) ? 1 : -1;
  }
  $arr1 = array("c" => "CSS", "h" => "HTML", "p" => "PHP");
  $arr2 = array("c" => "CSS", "h" => "HTML", "p" => "Python");
  
  $result=array_diff_uassoc($arr1,$arr2,"key_compare_function1");
  print_r($result);
?>
</pre>

Output:

Array
(
    [p] => PHP
)

Example 2:

<pre>
<?php
function key_compare_function2($a,$b){
    if ($a === $b){
      return 0;
      }
      return ($a>$b) ? 1 : -1;
  }
  $arr1 = array("c" => "CSS", "h" => "HTML", "j" => "JavaScript", "p" => "PHP");
  $arr2 = array("c" => "CSS", "h" => "HTML", "j" => "jQuery");
  $arr3 = array("h" => "HTML", "m" => "MySQL", "p" => "Python");
  
  $result=array_diff_uassoc($arr1, $arr2, $arr3, "key_compare_function2");
  print_r($result);
?>
</pre>

Output:

Array
(
    [j] => JavaScript
    [p] => PHP
)

Example 3:

<pre>
<?php
function key_compare_function3($a,$b){
    if ($a === $b){
      return 0;
      }
      return ($a>$b) ? 1 : -1;
  }
  $arr1 = array("c" => "CSS", "h" => "HTML", "p" => "PHP");
  $arr2 = array("C" => "CSS", "H" => "HTML", "j" => "JavaScript");
  
  $result=array_diff_uassoc($arr1, $arr2, "key_compare_function3");
  print_r($result);
?>
</pre>

Output:

Array
(
     => CSS
    [h] => HTML
    [p] => PHP
)

Example 4:

<pre>
<?php
$array1 = ['CSS' => "", 'JavaScript' => "", 'PHP' => "", 'Python' => "", 'Java' => ""];
$array2 = ['Django' => "", 'Bootstrap' => "", 'React' => "", 'Laravel' => ""];

function frameworkCheck($a, $b) {
$framework = [
    'CSS' => 'Bootstrap',
    'JavaScript' => 'React',
    'PHP' => 'Laravel',
    'Python' => 'Django'
];

if (isset($framework[$a])) {
    return $framework[$a] != $b;
} elseif(isset($framework[$b])) {
    return $framework[$b] != $a; 
}
return true;
}
$result = array_diff_uassoc($array1, $array2, 'frameworkCheck');
print_r($result);
?>
</pre>

Output:

Array
(
    [Java] => 
)

Explanation:

Here, the keys of the two functions array1 and array1 are different. By the $framework array, we mention the framework of each language. So the key comparison function (here, frameworkCheck() function) returns 0 if there is a match otherwise, it results 1 (true).

Notes on array_diff_uassoc() Function:

  • The function considers keys as case sensitive. So, key “c” is different than the key “C”. Check example 3.

PHP Version Support:

PHP 5, PHP 7, PHP 8

Summary: PHP array_diff_uassoc() Function

The array_diff_uassoc() is one of the built-in PHP array functions that compares keys as well as values of one array with other that of other arrays using a key comparison callback function.

Reference:

https://www.php.net/manual/en/function.array-diff-uassoc.php