PHP Function Reference

PHP array_diff_assoc() Function



The PHP array_diff_assoc() function compares keys and values of an array against one or more other arrays and returns all elements of the first array which are not present in any of the other arrays.

Unlike array_diff() function, it uses array keys also for comparison.

Syntax

array_diff_assoc(array, arrays)

Parameters

array Required. Specify an array to compare from.
arrays Required. Specify one or more arrays to compare against.

Return Value

Returns an array containing all the entries from array which are not present in any of the other arrays.

Exceptions

NA.

Example:

The example below shows the usage of array_diff_assoc() function.

<?php
$Arr1 = array(0, 1, 2, 3);
$Arr2 = array('00', '01', 2, 3);

//comparing Arr1 against Arr2
$Arr1_diff_Arr2 = array_diff_assoc($Arr1, $Arr2);
print_r($Arr1_diff_Arr2);
?>

The output of the above code will be:

Array
(
    [0] => 0
    [1] => 1
)

Example:

Consider one more example which illustrates on usage of array_diff_assoc() function.

<?php
$Arr1 = array("a"=>"Red", 
              "b"=>"Blue",
              "c"=>"Green");
$Arr2 = array("a"=>"Red", "Blue", "Green");

//comparing Arr1 against Arr2
$Arr1_diff_Arr2 = array_diff_assoc($Arr1, $Arr2);
print_r($Arr1_diff_Arr2);
?>

The output of the above code will be:

Array
(
    [b] => Blue
    [c] => Green
)

❮ PHP Array Reference