Latest web development tutorials

PHP array_diff_assoc () function

PHP Array Reference Complete PHP Array Reference

Examples

Compare two arrays of key names and values, and returns the difference set:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","b"=>"green","c"=>"blue");

$result=array_diff_assoc($a1,$a2);
print_r($result);
?>

Running instance »

Definition and Usage

array_diff_assoc () function is used to compare two (or more) of the array keys and values, and returns the set difference.

This function compares two (or more) of the array keys and values, and returns a difference of arrays, the array includes all the compared array (array1), but not in any other parameter array (array2 or array3 etc.) the key names and values.


grammar

array_diff_assoc( array1,array2,array3... );

参数 描述
array1 必需。与其他数组进行比较的第一个数组。
array2 必需。与第一个数组进行比较的数组。
array3,... 可选。与第一个数组进行比较的其他数组。

technical details

return value: Returns a difference of arrays, the array includes all the compared array (array1), but not in any other parameter array (array2 or array3 etc.) in key names and values.
PHP version: 4.3+


More examples

Example 1

Compare two arrays of key names and values, and returns the difference set:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("e"=>"red","f"=>"green","g"=>"blue");

$result=array_diff_assoc($a1,$a2);
print_r($result);
?>

Running instance »

Example 2

Comparison of three arrays of key names and values, and returns the difference set:

<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue","d"=>"yellow");
$a2=array("a"=>"red","f"=>"green","g"=>"blue");
$a3=array("h"=>"red","b"=>"green","g"=>"blue");

$result=array_diff_assoc($a1,$a2,$a3);
print_r($result);
?>

Running instance »


PHP Array Reference Complete PHP Array Reference