Wednesday, 24 September 2014

array_udiff_uassoc in PHP

array_udiff_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, a positive number (1) if the returned array should contain this element, and 0, or a negative number (-1), if not.

Syntax

array_udiff_uassoc(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 arrayvalue.

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"=>"Thursday" );
print_r(array_udiff_uassoc($daysarr1,$daysarr2,"myfunc_key","myfunc_value"));
?>

Output:


Array ( 
        [c] = > Tuesday 
       )

0 comments:

Post a Comment