PHP array_diff_assoc() Function
The array_diff_assoc()
function is an inbuilt function in PHP that compares two or more arrays and returns the differences, considering both keys and values. This function was introduced in PHP 4.0.
Syntax
array array_diff_assoc ( array $array1 , array $array2 [, array $... ] );
Parameters
Parameter | Description | Is Compulsory |
---|---|---|
array1 | The first array to compare. | Compulsory |
array2 | The array to compare against. | Compulsory |
... | Additional arrays to compare against. | Optional |
Return Values
The array_diff_assoc()
function returns an array containing all elements from array1
that are not present in any of the other arrays, considering both keys and values.
PHP Example
<?php
$array1 = array("a" => "red", "b" => "green", "c" => "blue");
$array2 = array("a" => "red", "c" => "yellow", "d" => "pink");
print_r(array_diff_assoc($array1, $array2));
?>
Output
Array
(
[b] => green
[c] => blue
)
(
[b] => green
[c] => blue
)
PHP Example
<?php
$array1 = array("a" => "apple", "b" => "banana", "c" => "cherry");
$array2 = array("b" => "banana", "d" => "dragonfruit");
$array3 = array("c" => "cherry", "e" => "elderberry");
print_r(array_diff_assoc($array1, $array2, $array3));
?>
Output
Array
(
[a] => apple
)
)