array_intersect_ukey


php128 apg

COMPUTES the intersection of ARRAYS using a callback function on the keys for comparison.





This function returns an ARRAY containing all the entries from $array1 which have matching keys that are present in all the arguments.

The $key_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_intersect_ukey 
arr $array1 arr $array2 [, 
                                         
arr $arra3, ..., arr $arrayN ], 
                                           callable 
$key_compare_func )
 

where,

$array1 The first ARRAY to keys check

$array2 
The second ARRAY to compare keys against

$array3 
The third ARRAY to compare keys against

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

$arrayN The last ARRAY to compare keys against

$key_compare_func 
The comparison function

?> 

$array1


The first ARRAY - keys to check.



$array2


The second ARRAY - to compare keys against.



$array3


The third ARRAY - to compare keys against.



$arrayN


The last ARRAY - to compare keys against.



$key_compare_func


The callback comparison function.



  1 EXERCISE   

<?php

function ukcompare ($k1$k2)
{
if (
$k1 === $k2)
    {
    return 
false;
    }
    elseif(
$k1 !== $k2)
    {
    return 
true;
    }
    else
    {
    return 
NULL;
    }
}

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

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

$arruk01ab array_diff_ukey ($arruk01a
                                                
$arruk01b
                                               
'ukcompare');
echo 
'FIRST ARRAY:<br>';
print_r($arruk01a);

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

echo 
'<br><br><br>RESULTING ARRAY:<br>';
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 ( [n] => number [o] => object [R] => resource )

The comparison between the two ARRAYS was made considering, ONLY, their respective keys.

Keys in UPPERCASE and/or lower case letters are considered as different.

The resulting ARRAY keys belong EXCLUSIVELY to the FIRST ARRAY.

The values of the RESULT ARRAY have the same values as those of the equivalent keys in the first ARRAY.

Comparative CRITERIA were established by the callback function.


  2 EXERCISE   

<?php

/*
* Function is implemented in ext/standard/array.c
*/

function key_compare_func($key1$key2) {
    if (
$key1 == $key2) return 0;
    else if (
$key1 $key2) return 1;
    else return -
1;
}
$array1 = array('blue' => 1'red' => 2
                        
'green' => 3'purple' => 4);
$array2 = array('green' => 5'blue' => 6
                        
'yellow' => 7'cyan' => 8);
                        
echo 
'FIRST ARRAY -> $array1:<br>';
print_r($array1);

echo 
'<br><br>SECOND ARRAY -> $array2:<br>';
print_r($array2);
                        
echo 
'<br><br><br>RESULTING ARRAY:<br>';                        
var_dump(array_intersect_ukey($array1
                                                  
$array2
                                                 
'key_compare_func'));

?>

  3 EXERCISE   

<?php

echo "Testing array_intersect_ukey() : usage variation.";

//Initialize variables
$arr_default_int = array(1);
$arr_float = array(=> 1.00
                        
1.00 => 2.00
                        
2.00 => 3.00);
$arr_string = array('0' => '1'
                               
'1' => '2'
                               
'2' => '3');
$arr_string_float = array('0.00' => '1.00'
                                       
'1.00' => '2.00');

echo 
'<br><br>The given $arr_default_int:<br>';
print_r($arr_default_int);

echo 
'<br><br>The given $arr_float:<br>';
print_r($arr_float);


echo 
'<br><br>The given $arr_string:<br>';
print_r($arr_string);

echo 
'<br><br>The given $arr_string_float:<br>';
print_r($arr_string_float);

//Call back function
function key_compare_func($key1$key2)
{
    if (
$key1 == $key2)
        return 
0;
    else
        return (
$key1 $key2)? 1:-1;
}

echo 
"<br><br>Result of 
integers and floating point intersection:<br>"
;
var_dumparray_intersect_ukey($arr_default_int
                                                   
$arr_float
                                                  
"key_compare_func") );

echo 
"<br><br>Result of 
integers and strings containing integers intersection::<br>"
;
var_dumparray_intersect_ukey($arr_default_int
                                                   
$arr_string
                                                  
"key_compare_func") );

echo 
"<br><br>Result of 
integers and strings containing floating points intersection:<br>"
;
var_dumparray_intersect_ukey($arr_default_int
                                                  
$arr_string_float
                                                 
"key_compare_func") );
                                                 
?>

  4 EXERCISE   

<?php

echo "Testing array_intersect_ukey() : usage variation.";

//Initialize variables
$arr_float = array(0.00 => 1.001.00 => 2.00);
$arr_string = array('0' => '1''1' => '2''2' => '3');
$arr_string_float = array('0.00' => '1.00''1.00' => '2.00');

echo 
'<br><br>The given $arr_float:<br>';
print_r($arr_float);

echo 
'<br><br>The given $arr_string:<br>';
print_r($arr_string);


echo 
'<br><br>The given $arr_string:<br>';
print_r($arr_string);

echo 
'<br><br>The given $arr_string_float:<br>';
print_r($arr_string_float);

//Call back function
function key_compare_func($key1$key2)
{
if (
$key1 == $key2)
return 
0;
else
return (
$key1 $key2)? 1:-1;
}

echo 
"<br><br>Result of 
floating points and strings 
                containing integers intersection:<br>"
;
var_dumparray_intersect_ukey($arr_float
                                                   
$arr_string
                                                  
'key_compare_func') );

echo 
"<br><br>Result of 
floating points and strings 
                 containing floating point intersection:<br>"
;
var_dumparray_intersect_ukey($arr_float
                                                   
$arr_string_float
                                                  
'key_compare_func') );
                                                       
?>

  5 EXERCISE   

<?php

echo "Testing array_intersect_ukey() : usage variation.";

//Initialize variables
$arr1_string_int = [ '0' => '1''1' => '2' ];
$arr2_string_int = [ '0' => '1''1' => '3' ];
$arr1_string_float = [ '0.00' => '1.00''1.00' => '2.00' ];
$arr2_string_float = [ '0.00' => '1.00''1.00' => '3.00' ];

echo 
'<br><br>The given $arr1_string_int:<br>';
print_r($arr1_string_int);

echo 
'<br><br>The given $arr2_string_int:<br>';
print_r($arr2_string_int);

echo 
'<br><br>The given $arr1_string_float<br>';
print_r($arr1_string_float);

echo 
'<br><br>The given $arr2_string_float:<br>';
print_r($arr2_string_float);

//Call back function
function key_compare_func($key1$key2)
{
    if (
$key1 == $key2)
        return 
0;
    else
        return (
$key1 $key2)? 1:-1;
}

echo 
"<br><br><br>Result of 
strings containing integers intersection:<br>"
;
var_dumparray_intersect_ukey($arr1_string_int
                                                   
$arr2_string_int
                                                  
'key_compare_func') );

echo 
"<br><br>Result of strings containing 
                         floating points intersection:<br>"
;
var_dumparray_intersect_ukey($arr1_string_float
                                                   
$arr2_string_float
                                                  
'key_compare_func') );

echo 
"<br><br>Result of 
strings containing integers and strings containing 
                          floating points intersection:<br>"
;
var_dumparray_intersect_ukey($arr1_string_int
                                                   
$arr2_string_float
                                                  
'key_compare_func') );
?>