YouTip LogoYouTip

Func Array Diff Uassoc

```html PHP array_diff_uassoc() Function | Rookie Tutorial

PHP array_diff_uassoc() Function | Rookie Tutorial

Rookie Tutorial - Learning is not just technology, but also dreams!

PHP Tutorial

PHP Forms

PHP Advanced Tutorial

PHP 7 New Features

PHP Database

PHP array_diff_uassoc() Function

array_diff_uassoc() function compares the keys and values of two (or more) arrays and returns the difference.

Note: This function uses a user-defined callback function to compare the keys (the keys are used for comparison).

This function compares the keys and values of two (or more) arrays and returns an array containing all entries from array1 that are not present in any of the other arrays.

Syntax

array_diff_uassoc(array1, array2, array3..., myfunction)

Parameter Values

ParameterDescription
array1Required. The array to compare from
array2Required. An array to compare against
array3,...Optional. More arrays to compare against
myfunctionRequired. A string that defines a callable comparison function. The comparison function must return an integer < 0, = 0, or > 0 if the first argument is respectively less than, equal to, or greater than the second.

Technical Details

Return Value:Returns an array containing all entries from array1 that are not present in any of the other arrays.
PHP Version:5.0+

Example 1

Compare the keys and values of two arrays (using a user-defined function to compare the keys), 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"=>"red","b"=>"green","c"=>"blue");

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

Run Example Β»

Example 2

Compare three arrays, using a user-defined function to compare the keys:

<?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"=>"red","b"=>"green","d"=>"blue");
$a3=array("e"=>"yellow","a"=>"red","d"=>"blue");

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

Run Example Β»

Example 3

Using an anonymous function (available in PHP 5.3+):

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");

$result=array_diff_uassoc($a1,$a2, function($a,$b) {
    if ($a===$b) return 0;
    return ($a>$b)?1:-1;
});
print_r($result);
?>

Run Example Β»

```
← Func Array Diff UkeyFunc Array Diff Key β†’