PHP array_diff_key () function
Examples
Compare two arrays of keys, and returns the difference set:
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");
$result=array_diff_key($a1,$a2);
print_r($result);
?>
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("a"=>"red","c"=>"blue","d"=>"pink");
$result=array_diff_key($a1,$a2);
print_r($result);
?>
Running instance »
Definition and Usage
array_diff_key () function is used to compare two (or more) key name of the array, and returns the set difference.
This function compares two (or more) key name of the array, 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. key name) in the.
grammar
array_diff_key( 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 the key name. |
---|---|
PHP version: | 5.1+ |
More examples
Example 1
Compare two values of the array keys, and returns the difference set:
<?php
$a1=array("red","green","blue","yellow");
$a2=array("red","green","blue");
$result=array_diff_key($a1,$a2);
print_r($result);
?>
$a1=array("red","green","blue","yellow");
$a2=array("red","green","blue");
$result=array_diff_key($a1,$a2);
print_r($result);
?>
Running instance »
Example 2
Comparison of three arrays of keys, and returns the difference set:
<?php
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("c"=>"yellow","d"=>"black","e"=>"brown");
$a3=array("f"=>"green","c"=>"purple","g"=>"red");
$result=array_intersect_key($a1,$a2,$a3);
print_r($result);
?>
$a1=array("a"=>"red","b"=>"green","c"=>"blue");
$a2=array("c"=>"yellow","d"=>"black","e"=>"brown");
$a3=array("f"=>"green","c"=>"purple","g"=>"red");
$result=array_intersect_key($a1,$a2,$a3);
print_r($result);
?>
Running instance »
![PHP Array Reference](/images/up.gif)