array_udiff


php128 apg

COMPUTES the difference of ARRAYS using a callback function for data comparison.





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

This function is like array_diff except the comparison is done on the values instead of the keys.

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 
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 udcompare ($k1$k2)
{
if (
$k1 === $k2)
    {
    return 
0;
    }
    elseif (
$k1 $k2)
    {
    return 
1;
    }
    else
    {
    return -
1;
    }
}

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

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

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

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

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

$arruV01ab array_udiff ($arruV01a
                                         
$arruV01b
                                        
'udcompare');
print_r($arruV01ab);

?>

 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 ( [n] => number [o] => object )

Verify that all values of RESULTING ARRAY belong to FIRST ARRAY with the same keys as this one.


  2 EXERCISE   

<?php

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

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

$arruV02b = [ "S" => 'string'"a" => 'ARRAY'"r" => 'RESOURCE' ];

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

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

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

$arruV02ab array_udiff ($arruV02a
                                          
$arruV02b
                                         
'udcompare');
print_r($arruV02ab);

?>

 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 ( [a] => array [n] => number [o] => object [r] => resource )

Verify that all values of RESULTING ARRAY belong to FIRST ARRAY with the same keys as this one.

This function is case-sensitive for both keys and values.