PHP array_diff_key() function
In this chapter you will learn:
- 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
Definition
The array_diff_key() function compares the keys of two or more arrays, and returns entries from array1 that are not present in array2 or array3...
Syntax
PHP array_diff_key() function has the following syntax.
array_diff_key(array1,array2,array3...);
Parameter
Parameter | Is Required | Description |
array1 | Required. | Array to compare from |
array2 | Required. | An array to compare against |
array3,... | Optional. | More arrays to compare against |
Example
Compare the keys of two indexed arrays, and return the differences:
<?php//from ja v a2s .c o m
$a1=array("red","green","blue","java2s.com");
$a2=array("red","green","blue");
$result=array_diff_key($a1,$a2);
print_r($result);
?>
The code above generates the following result.
Example 2
Compare the keys of two associative arrays, and return the differences:
<?php// j a v a 2 s . com
$a1=array("a"=>"A","b"=>"Bed","c"=>"Cat");
$a2=array("a"=>"A","c"=>"Cat","d"=>"Dog","j"=>"java2s.com");
$result=array_diff_key($a1,$a2);
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_uassoc() function
- Syntax for PHP array_diff_uassoc() function
- Parameter for PHP array_diff_uassoc() function
- Example - Compare the keys and values of two arrays use a user-defined function to compare the keys
Home » PHP Tutorial » PHP Array Functions