PHP array_diff_uassoc() Function | Rookie Tutorial
- Home
- HTML
- JavaScript
- CSS
- Vue
- React
- Python3
- Java
- C
- C++
- C#
- AI
- Go
- SQL
- Linux
- VS Code
- Bootstrap
- Git
- Local Bookmarks
PHP Tutorial
- PHP Tutorial
- PHP Introduction
- PHP Installation
- PHP Syntax
- PHP Variables
- PHP echo/print
- PHP EOF(heredoc)
- PHP Data Types
- PHP Type Comparison
- PHP Constants
- PHP Strings
- PHP Operators
- PHP If...Else
- PHP Switch
- PHP Arrays
- PHP Array Sort
- PHP Superglobals
- PHP While Loop
- PHP For Loop
- PHP Functions
- PHP Magic Constants
- PHP Namespaces
- PHP OOP
- PHP Quiz
PHP Forms
- PHP Forms
- PHP Form Validation
- PHP Forms - Required Fields
- PHP Forms - Validate E-mail/URL
- PHP Complete Form Example
- PHP $_GET
- PHP $_POST
PHP Advanced Tutorial
- PHP Multidimensional Arrays
- PHP Date
- PHP Includes
- PHP File
- PHP File Upload
- PHP Cookie
- PHP Session
- PHP E-mail
- PHP Secure E-mail
- PHP Error
- PHP Exception
- PHP Filter
- PHP Advanced Filters
- PHP JSON
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
| Parameter | Description |
|---|---|
| array1 | Required. The array to compare from |
| array2 | Required. An array to compare against |
| array3,... | Optional. More arrays to compare against |
| myfunction | Required. 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);
?>
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);
?>
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);
?>
```
YouTip