English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

PHP Basic Tutorial

PHP Advanced Tutorial

PHP & MySQL

PHP Reference Manual

PHP array_diff_assoc() Function Usage and Example

PHP Array Function Manual

Definition and Usage

array_diff_assoc()function compares two (or more) arrays and returns the difference.

This function compares the keys and values of two (or more) arrays and returns an array1entry but in array2or array3does not exist in the array,... etc.

this function is different fromarray_diff()function, because array_diff() only compares values with other arrays, whilearray_diff_assoc()The function uses both keys and values to compare with other arrays.

Syntax

array array_diff_assoc(array $array1, array $array2 [, array $array3...] );

Parameter

Serial NumberParameters and Description
1

array1 (Required)

is the array to be compared

2

array2 (Required)

It is the array to be compared with the first array

3

array3(Optional)

It is the array to be compared with the first array

Return Value

The array_diff_assoc() function returns an array containing the elements of1where all values are unique, these values do not exist in any other array with the same key.

PHP version

This function was first introduced in PHP version4.3introduced in version 5.3.0 of PHP.

Online Example

Try the following example. Both arrays contain "a" => "orange" and "c" => "banana", so they will not appear in the result-

<?php
   $input1 = array("a" => "orange", "b" => "mango", "c" => "banana");
   $input2 = array("a" => "orange", "b" => "apple", "c" => "banana");
   print_r(array_diff_assoc($input1, $input2);
?>
Test to see‹/›

Output Result:

Array
(
    [b] => mango
)

Online Example

Here, two arrays have different keys and corresponding values for all pairs, for example, "a" => "orange" does not exist in the second array, similarly, other key-value pairs also do not exist in the second array, so they will be available in the result-

<?php
   $input1 = array("a" => "orange", "b" => "mango", "c" => "banana");
   $input2 = array("a" => "banana", "b" => "apple", "c" => "orange");
   print_r(array_diff_assoc($input1, $input2);
?>
Test to see‹/›

Output Result:

Array
(
    [a] => orange
    [b] => mango
    [c] => banana
)

Online Example

The following example illustrates that only when (string)$elem1 ===(string)$elem2only when two values in the key=>value pair are considered equal.

<?php
    $input1 = array(0, 5, 20);
    $input2 = array("00", "05", "20);
    $result = array_diff_assoc($input1, $input2);
    print_r($result);
?>
Test to see‹/›

Output Result:

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

PHP Array Function Manual