Sunday, 25 August 2013

PHP Function array_diff_ukey()

Syntax

array_diff_ukey ( $array1, $array2 [, $array3...,callback $key_compare_func] );

Definition and Usage

Compares the keys from array1 against the keys from array2 and returns the difference. This function is like array_diff() except the comparison is done on the keys instead of the values.
Unlike array_diff_key() an user supplied callback function is used for the indices comparision, not internal function.

Paramters

ParameterDescription
array1Required. The first array is the array that the others will be compared with.
array2Required. An array to be compared with the first array
array3Optional. An array to be compared with the first array
key_compare_funcRequired. callback function to use. The callback function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.

Return Values

Returns an array containing all the entries from array1 that are not present in any of the other arrays.

Example

Try out following example:
<?php
function key_compare_func($v1,$v2) 
{
if ($v1===$v2)
 {
 return 0;
 }
if ($v1>v2)
 {
 return 1;
 }
else
 {
 return -1;
 }
}
$array1 = array(0=>"banana", 1=>"orange", 2=>"grapes");
$array2 = array(3=>"apple",1=>"apricot", 5=>"mango");
print_r(array_diff_ukey($array1,$array2,"key_compare_func"));
?> 
This will produce following result:
Array ( [0]=>banana [2]=>grapes )

No comments:

Post a Comment