PHP array_diff_assoc() Function
In this chapter you will learn:
- Description for PHP array_diff_assoc() Function
- Syntax for PHP array_diff_assoc() Function
- Parameter for PHP array_diff_assoc() Function
- Example - Compare the keys and values of two arrays, and return the differences
- Example - Compare the keys and values of three arrays, and return the differences
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
Parameter | Is Required | Description |
---|---|---|
array1 | Required. | The array to compare from |
array2 | Required. | 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 ja v a2 s. c om*/
$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// j a v a 2s.c o m
$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.
Next chapter...
What you will learn in the next chapter:
- Definition for PHP array_diff_key() function
- Syntax for PHP array_diff_key() function
- Parameter for PHP array_diff_key() function
- Example - Compare the keys of two indexed arrays, and return the differences
- Example - Compare the keys of two associative arrays, and return the differences
Home » PHP Tutorial » PHP Array Functions