Array Functions

PHP array_diff_assoc() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_diff_assoc() function compares the elements of two or more arrays and returns the differences. Unlike array_diff(), the array keys are also used in the comparison.

The following table summarizes the technical details of this function.

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

Syntax

The basic syntax of the array_diff_assoc() function is given with:

array_diff_assoc(array1, array2, ...);

The following example shows the array_diff_assoc() function in action.

<?php
// Sample arrays
$array1 = array("a"=>"apple", "b"=>"ball", "c"=>"cat", "dog");
$array2 = array("a"=>"apple", "banana", "papaya");

// Computing the difference
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>

Parameters

The array_diff_assoc() function accepts the following parameters.

Parameter Description
array1 Required. Specifies the array to compare from.
array2 Required. Specifies an array to compare against.
... Optional. Specifies more arrays to compare against.

More Examples

Here're some more examples showing how array_diff_assoc() function actually works:

In the following example this function compares an array against two other arrays.

<?php
// Sample arrays
$array1 = array("a"=>"apple", "b"=>"ball", "c"=>"cat", "dog");
$array2 = array("a"=>"apricot", "c"=>"cat");
$array3 = array("a"=>"apple", "b"=>"banana");

// Computing the difference
$result = array_diff_assoc($array1, $array2, $array3);
print_r($result);
?>

The two keys and values from the key=>value pairs are considered equal if their string representation are same, i.e., (string) $elem1 === (string) $elem2. Let's take a look at an example:

<?php
// Sample arrays
$array1 = array(0, 1, 2, 5, 7);
$array2 = array("00", "1", 2, "05", 8);

// Computing the difference
$result = array_diff_assoc($array1, $array2);
print_r($result);
?>
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties