Thursday, 25 September 2014

array_uintersect_uassoc in PHP

array_uintersect_uassoc() function is used to compare two or more arrays, in two user-defined functions, and then returns an array with the elements from the first array, if the user-defined functions allow it.
The first user-defined function compares array keys, and the second compares array values, and both returns a numeric value, 0 if the returned array should contain this element.

Syntax:

array_uintersect_assoc(array1,array2,array3...,function1,function2)
array1 : Required. The first array is the array that the others will be compared with
array2 : Required. An array to be compared with the first array
array3 : Optional. An array to be compared with the first array
function1 : Required. The name of the user-made function that compares the array keys
function2 : Required. The name of the user-made function that compares the array values
Tip: You can compare the first array with one array, or as many as you like.
Note: For comparison, the key is used in the first function and the value is used in the second. They are both user-made functions.

Example:

<?php 
function myfunc_key($par1,$par2)
{
if($par1===$par2)
{ 
return 0;
}
return 1;
}
function myfunc_value($par1,$par2)
{
if($par1===$par2)
{ 
return 0;
}
return 1;
}
$daysarr1 = array( "a"=>"Sunday","b"=>"Monday","c"=>"Tuesday" );
$daysarr2 = array( "a"=>"Sunday","b"=>" Monday","c"=>"Monday" );
print_r(array_uintersect_uassoc($daysarr1,$daysarr2,"myfunc_key","myfunc_value"));
?>

The output of the code above will be:

Array ( [a] = > Sunday )

0 comments:

Post a Comment