PHP array_diff_assoc()Functions

PHP Array 参考手册Complete PHP Array Reference Manual

Examples

Compare the keys and values of two arrayskeys and values, and return the difference:

<?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);
?>

Run Example »

Definition and Usage

The array_diff_assoc() function is used to compare the keys and values of two (or more) arrayskeys and values, and return the difference.

This function compares the keys and values of two (or more) arrays, and returns a difference array that includes all the keys and values from the compared array (array1) that are not present in any other parameter array (array2orarray3etc.).


Syntax

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

Parameter Description
array1 Required. The first array to compare with other arrays.
array2 Required. The array to compare with the first array.
array3,... Optional. Other arrays to compare with the first array.

Technical Details

Return value: Returns a difference array that includes all the keys and values from the compared array (array1) that are not present in any other parameter array (array2orarray3etc.).
PHP Version: 4.3+


More Examples

Example 1

Compare the keys and values of two arrayskeys and values, and return the difference:

<?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);
?>

Run Example »

Example 2

Compare the keys and values of three arrayskeys and values, and return the difference:

<?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);
?>

Run Example »


PHP Array 参考手册Complete PHP Array Reference Manual Other Extensions