array_diff_uassoc


php128 apg

COMPUTES the difference between ARRAYS with additional index check which is performed by a user supplied callback function.





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

Multiple occurrences in $array1 are all treated the same way.

In order for two elements to be considered identical, they must have the same value.

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

Deeper dimensions can be made using this function to compare identical keys.

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.

A CALLBACK occurs when the call originator is immediately called back in a second response and when the call originator is immediately called back in a second response.

The call back process can be repeated several times.

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



<?php

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

$key_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.



$key_compare_func


The callback comparison function.



  1 EXERCISE   

<?php

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

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

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

echo 
'The given FIRST ARRAY:<br>';
print_r($arruk001);

echo 
'<br><br>The given SECOND ARRAY:<br>';
print_r($arruk002);

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

$arruk012 array_diff_uassoc ($arruk001
                                       
$arruk002
                                       
'uacompare');
print_r($arruk012);

?> 

 RESULT   

The given FIRST ARRAY

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

The given SECOND ARRAY

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


RESULTING ARRAY

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


  2 EXERCISE   

<?php

/*
* array array_diff_uassoc 
* ( array $array1, array $array2 [, 
* array $..., callback $key_compare_func] )
* Function is implemented in ext/standard/array.c
*/

function key_compare_func($a$b) {
    if (
$a === $b) {
        return 
0;
    }
    return (
$a $b) ? : -1;
}
$array1 = ["a" => "green""b" => "brown""c" => "blue""red"];
$array2 = ["a" => "green""yellow""red"];

echo 
'The given FIRST ARRAY:<br>';
print_r($array1);

echo 
'<br><br>The given SECOND ARRAY:<br>';
print_r($array2);

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

$result array_diff_uassoc($array1
                                           
$array2
                                             
"key_compare_func");
var_dump($result);

?>

  5 EXERCISE   

<?php

echo "Testing array_diff_uassoc() : usage variation.<br><br>";

// Initialise function arguments 
// not being substituted (if any)
$input_array = array(=> '0'10 => '10'
                               -
10 => '-10'20 =>'20', -20 => '-20');
$float_indx_array = array(0.0 => '0.0'10.5 => '10.5',
                                      -
10.5 => '-10.5'0.5 => '0.5');

echo 
'The given $input_array:<br>';
var_dump($input_array);

echo 
'<br><br>The given $float_indx_array<br>';
var_dump($float_indx_array);

echo 
"<br><br>Testing array_diff_uassoc() function
                                         with float indexed array:<br>"
;
var_dumparray_diff_uassoc($input_array
                                              
$float_indx_array
                                             
"strcasecmp") );
echo 
'<br><br>';
var_dumparray_diff_uassoc($float_indx_array
                                              
$input_array
                                             
"strcasecmp") );

?>

  6 EXERCISE   

<?php

echo "Testing array_diff_uassoc() : usage variation.<br><br>";

// Initialise function arguments 
// not being substituted (if any)
$input_array = array(=> '0'=> '1', -10 => '-10'
                                        
'true' => 1'false' => 0);
$boolean_indx_array = array(true => 'boolt'false => 'boolf'
                                             
TRUE => 'boolT'FALSE => 'boolF');

echo 
"Testing array_diff_key() function with 
                                                  float indexed array:<br>"
;
var_dumparray_diff_uassoc($input_array
                                              
$boolean_indx_array
                                             
"strcasecmp") );
echo 
'<br><br>';                                             
var_dumparray_diff_uassoc($boolean_indx_array
                                               
$input_array
                                              
"strcasecmp") );

?>

  7 EXERCISE   

<?php

echo "Testing array_diff_uassoc() : usage variation.<br><br>";

// Initialise function arguments not being substituted (if any)
$input_array = array(10 => '10'"" => '');

//get an unset variable
$unset_var 10;
unset (
$unset_var);

$input_arrays = array(
'NULL indexed' => array(NULL => NULLnull => null),
'UNDEFINED indexed' => array(@$undefined_var
                                 
=> @$undefined_var),
'UNSET indexed' => array(@$unset_var => @$unset_var),
);

echo 
'The given $input_array:<br>';
var_dump($input_array);

echo 
'<br><br>The given $input_arrays:<br>';
var_dump($input_arrays);

foreach(
$input_arrays as $key =>$value) {
echo 
"<br><br>$key:<br>";
var_dumparray_diff_uassoc($input_array
                                              
$value"strcasecmp") );
var_dumparray_diff_uassoc($value
                                              
$input_array"strcasecmp") );
}

?>

  8 EXERCISE   

<?php

echo "Testing array_diff_uassoc() : usage variation.<br><br>";

//Initialize variables
$ref_var 'a';
$array1 = array('a'$ref_var);
$array2 = array('a' => 1, &$ref_var);

echo 
"With referenced variable \$ref_var has value '$ref_var':<br>";
var_dumparray_diff_uassoc($array1
                                              
$array2"strcasecmp") );
echo 
'<br><br>';
var_dumparray_diff_uassoc($array2
                                              
$array1"strcasecmp") );

// re-assign reference variable to different value
$ref_var 10.00;
echo 
"<br><br>With referenced 
                 variable \$ref_var value changed to 
$ref_var:<br>";
var_dumparray_diff_uassoc($array1
                                              
$array2"strcasecmp") );
echo 
'<br><br>';
var_dumparray_diff_uassoc($array2
                                              
$array1"strcasecmp") );

//When array are refenced
$array2 = &$array1;
echo 
"<br><br>When \$array2 is referenced to \$array1:<br>";
var_dumparray_diff_uassoc($array1
                                              
$array2"strcasecmp") );
echo 
'<br><br>';
var_dumparray_diff_uassoc($array2
                                              
$array1"strcasecmp") );

?>

  10 EXERCISE   

<?php

echo "Testing array_diff_uassoc() : usage variation.<br><br>";

//Initialize variables
$arr_default_int = array(1234);
$arr_float = array(=> 1.001.00 => 2.00
                                            
2.00 => 3.003.00 => 4.00);

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

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


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

echo 
"<br><br>Comparing 
                  integers and floating point numbers.<br>"
;
echo 
'After 
   array_diff_uassoc($arr_default_int, 
                               $arr_float, "key_compare_func"):<br>'
;
var_dumparray_diff_uassoc($arr_default_int
                                              
$arr_float
                                             
"key_compare_func") );
echo 
'<br><br>After 
 array_diff_uassoc($arr_float, 
                             $arr_default_int, "key_compare_func"):<br>'
;
var_dumparray_diff_uassoc($arr_float
                                               
$arr_default_int
                                              
"key_compare_func") );

?>

  11 EXERCISE   

<?php

echo "Testing array_diff_uassoc() : usage variation.<br><br>";

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

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

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

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

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

echo 
"<br<br>Comparing floating points and strings 
                                     containing integers:<br>"
;
var_dumparray_diff_uassoc($arr_float
                                              
$arr_string_int
                                             
"key_compare_func") );
echo 
'<br><br>';
var_dumparray_diff_uassoc($arr_string_int
                                              
$arr_float
                                             
"key_compare_func") );

echo 
"<br><br>Comparing floating points and strings 
                                     containing floating point:<br>"
;
var_dumparray_diff_uassoc($arr_float
                                              
$arr_string_float
                                             
"key_compare_func") );
echo 
'<br><br>';
var_dumparray_diff_uassoc($arr_string_float
                                              
$arr_float
                                             
"key_compare_func") );

?>

  12 EXERCISE   

<?php

echo "Testing array_diff_uassoc() : usage variation.<br><br>";

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

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

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

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

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

echo 
"<br><br>Comparing floating points and 
                   strings containing integers:<br>"
;
var_dumparray_diff_uassoc($arr_float
                                              
$arr_string_int
                                             
"key_compare_func") );
echo 
'<br><br>';
var_dumparray_diff_uassoc($arr_string_int
                                              
$arr_float
                                             
"key_compare_func") );

echo 
"<br><br>Comparing floating points and 
                   strings containing floating point:<br>"
;
var_dumparray_diff_uassoc($arr_float
                                              
$arr_string_float
                                             
"key_compare_func") );
echo 
'<br><br>';
var_dumparray_diff_uassoc($arr_string_float
                                              
$arr_float
                                             
"key_compare_func") );

?>

  13 EXERCISE   

<?php

echo "Testing array_diff_uassoc() : usage variation.<br><br>";

//Initialize variables
$arr_string_int = array('1''2');
$arr_string_float = array('0' => '1.00''1.00' => '2.00');

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

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

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

echo 
"<br><br>Comparing strings containing integers and
                strings containing floating points:<br>"
;
var_dumparray_diff_uassoc($arr_string_int
                                               
$arr_string_float
                                              
"key_compare_func") );

echo 
'<br><br>';
var_dumparray_diff_uassoc($arr_string_float
                                               
$arr_string_int
                                              
"key_compare_func") );

?>

  14 EXERCISE   

<?php

echo "Testing array_diff_uassoc() : usage variation.<br><br>";

//Initialize variables
$arr_default_int = [123];
$arr_string_int = array('1''2');
$arr_string_float = array('0' => '1.00''1.00' => '2.00');

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

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

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

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

echo 
"<br><br>Comparing integers and 
                      strings containing an integers:<br>"
;
var_dumparray_diff_uassoc($arr_default_int
                                               
$arr_string_int
                                              
"key_compare_func") );
echo 
'<br><br>';
var_dumparray_diff_uassoc($arr_string_int
                                                
$arr_default_int
                                               
"key_compare_func") );

echo 
"<br><br>Comparing integers and 
                             strings containing floating points:<br>"
;
var_dumparray_diff_uassoc($arr_default_int
                                               
$arr_string_float
                                              
"key_compare_func") );

echo 
'<br><br>';
var_dumparray_diff_uassoc($arr_string_float
                                               
$arr_default_int
                                              
"key_compare_func") );

?>

  15 EXERCISE   

<?php

echo "Testing array_diff_uassoc() : usage variation.<br><br>";

// Initialise function arguments not being substituted (if any)
$input_array = array(10 => 1012 => 12);

$input_arrays = array(
      
'DECIMAL indexed' => array(10 => 10, -17 => -17),
      
'OCTAL indexed' => array( 012 => 10, -011 => -011,),
      
'HEXA  indexed' => array(0xA => 10, -0x7 => -0x7 ),
);

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

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

foreach(
$input_arrays as $key =>$value) {
      echo 
"<br><br>$key:<br>";
      
var_dumparray_diff_uassoc($input_array
                                                   
$value
                                                  
"strcasecmp") );
      echo 
'<br><br>'
      
var_dumparray_diff_uassoc($value
                                                     
$input_array
                                                    
"strcasecmp") );
}

?>