Array Functions

PHP array_intersect_assoc() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_intersect_assoc() function compares the elements of two or more arrays and returns the matches. Unlike array_intersect(), 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 present in all of the other arrays.
Version: PHP 4.3.0+

Syntax

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

array_intersect_assoc(array1, array2, ...);

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

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

// Computing the intersection
$result = array_intersect_assoc($array1, $array2);
print_r($result);
?>

Parameters

The array_intersect_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_intersect_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"=>"airplane", "b"=>"balloon", "c"=>"cat");
$array3 = array("a"=>"ant", "b"=>"banana", "c"=>"cat");

// Computing the intersection
$result = array_intersect_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, 7);

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