|
|
Php Array Function array_diff_ukey()
|
Tutorials » Php »
|
Topic |
What is array_diff_ukey() Function?
|
|
Explanation |
|
The "array_diff_ukey()" function is used to compute the difference of arrays using a callback function based on the keys of
the arrays used in comparison.
Syntax:
array_diff_ukey(array1,array2,array3...,function)
In the above syntax "array1" is the array to be compared with, "array2" is the array to be compared,
atleast two arrays should be there, "array 3" is optional. Comparison is done using the "function" specified.
Example
<?php
function key_compare_func($key1, $key2)
{
if ($key1 == $key2)
return 0;
else if ($key1 > $key2)
return 1;
else
return -1;
}
$array1 = array('blue'=> 1,'red'=> 2,'green'=> 3,'purple'=> 4);
$array2 = array('green'=> 5,'blue'=> 6,'yellow'=> 7,'cyan'=> 8);
var_dump(array_diff_ukey($array1, $array2, 'key_compare_func'));
?>
Result:
array(2)
{
["red"]=>
int(2)
["purple"]=>
int(4)
}
In the above example the "array_diff_ukey" checks for keys in two arrays "array1", "array2"
using the function "key_compare_func" based on the keys to display the elements that are not common in both
arrays, but present in "array1".
|
|
A Note |
Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial.
Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.
|
|
|
|