PHP array_diff_assoc() Function

Description

The array_diff_assoc() function compares the keys and values of two (or more) arrays, and returns the differences.

Syntax

PHP array_diff_assoc() Function has the following syntax.

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

Parameter

ParameterIs RequiredDescription
array1Required.The array to compare from
array2Required.An array to compare against
array3,...Optional.More arrays to compare against

Example 1

Compare the keys and values of two arrays, and return the differences:


<?php//from  w  w  w .  j  av  a  2  s.  com
$a1=array("R"=>"red","G"=>"green","B"=>"blue","Y"=>"yellow");
$a2=array("R"=>"red","G"=>"green");

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

The code above generates the following result.

Example 2

Compare the keys and values of three arrays, and return the differences:


<?php//w ww  .ja  v a2  s  . com
$a1=array("R"=>"red","G"=>"green","B"=>"blue","Y"=>"yellow");
$a2=array("R"=>"red","G"=>"green");
$a3=array("J"=>"java2s.com");

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

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions