array_udiff_assoc


php128 apg

COMPUTES the difference of ARRAYS with additional index check, using a callback function for data comparison.





This function only checks one dimension of a n-dimensional array.

This function returns an ARRAY containing all the entries from $array1 that are not present in any of the other arrays.

Note that the keys are used in the comparison unlike array_diff and array_udiff.

The comparison of arrays' data is performed by using an user-supplied callback.

In this aspect the behaviour is opposite to the behaviour of the function array_diff_assoc which uses internal function for comparison.

The $values_compare_func, 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.

Prior to PHP 7.0.0 this value is an INTEGER in the range -2147483648 to 2147483647.



<?php

arr array_udiff_assoc 
arr $array1 arr $array2 [, 
                                                         
arr $arra3, ..., arr $arrayN ],
                                                            callable 
$value_compare_func )
 

where,

$array1 The first ARRAY to compare from

$array2 
The second ARRAY to compare against

$array3 
The third ARRAY to compare against

. . . . . . . . . . . . . . .

$arrayN The last ARRAY to compare against

$value_compare_func 
The comparisson function

?> 

$array1


The first ARRAY - to compare from.



$array2


The second ARRAY - to compare against.



$array3


The third ARRAY - to compare against.



$arrayN


The last ARRAY - to compare against.



$value_compare_func


The callback comparison function.



  1 EXERCISE   

<?php

function udacompare ($k1$k2)
{
if (
$k1 === $k2)
    {
    return 
0;
    }
    elseif (
$k1 $k2)
    {
    return 
1;
    }
    else
    {
    return -
1;
    }
}

$arruk01a = [ "S" => 'string'"a" => 'array'
                                            
"n" => 'number'
                                            
"o" => 'object'"r" => 'resource' ];

$arruk01b = [ "s" => 'string'"A" => 'array'"r" => 'resource' ];

echo 
'FIRST ARRAY:<br>';
print_r($arruk01a);

echo 
'<br><br>SECOND ARRAY:<br>';
print_r($arruk01b);

echo 
'<br><br><br>RESULTING ARRAY:<br>';

$arruk01ab array_udiff_assoc ($arruk01a
                                                    
$arruk01b
                                                   
'udacompare');
print_r($arruk01ab);

?>

 RESULT   

FIRST ARRAY:
Array ( [S] => string [a] => array [n] => number [o] => object [r] => resource )

SECOND ARRAY:
Array ( [s] => string [A] => array [r] => resource )


RESULTING ARRAY:
Array ( [S] => string [a] => array [n] => number [o] => object )


  2 EXERCISE   

<?php

function udacompare ($k1$k2)
{
if (
$k1 === $k2)
    {
    return 
0;
    }
    elseif (
$k1 $k2)
    {
    return 
1;
    }
    else
    {
    return -
1;
    }
}

$arruk02a = [ "S" => 'string'"a" => 'array'
                                            
"n" => 'number'
                                            
"o" => 'object'"r" => 'resource' ];

$arruk02b = [ "s" => 'string'"a" => 'ARRAY'"r" => 'resource' ];

echo 
'FIRST ARRAY:<br>';
print_r($arruk02a);

echo 
'<br><br>SECOND ARRAY:<br>';
print_r($arruk02b);

echo 
'<br><br><br>RESULTING ARRAY:<br>';

$arruk02ab array_udiff_assoc ($arruk02a
                                                    
$arruk02b
                                                   
'udacompare');
print_r($arruk02ab);

?>

 RESULT   

FIRST ARRAY:
Array ( [S] => string [a] => array [n] => number [o] => object [r] => resource )

SECOND ARRAY:
Array ( [s] => string [a] => ARRAY [r] => resource )


RESULTING ARRAY:
Array ( [S] => string [a] => array [n] => number [o] => object )