phparraysassociativecustom-functionarray-difference

What is the difference between array_udiff_assoc() and array_diff_uassoc()?


What is the difference between array_udiff_assoc() and array_diff_uassoc()?

For array_udiff_assoc(), I have this code:

function myfunction($v1,$v2)
{
    if ($v1===$v2) {
        return 0;
    }
    return 1;
}
 
$a1 = ["a" => "Cat", "b" => "Dog", "c" => "Horse"];
$a2 = ["a" => "Cat", "b" => "Horse", "c" => "Dog"];
print_r(array_udiff_assoc($a1, $a2, "myfunction"));

result

Array ( [b] Dog [c] => Horse )

also array_diff_uassoc():

function myfunction($v1,$v2)
{
    if ($v1===$v2) {
        return 0;
    }
    return 1;
}
     
$a1 = ["a" => "Cat", "b" => "Dog", ​"c" => "Horse"];
​$a2 = ["a" => "Cat", "b" => "Horse", "c" => "Dog"];
​print_r(array_diff_uassoc($a1, $a2, "myfunction"));

The result is same as first one:

Array ( [b] Dog [c] => Horse )

If they have any difference, what is it? The PHP manual does not says that they are aliases of each other.


Solution

  • Perhaps a battery of test cases will clear up any confusion between these related native functions. I'll consistently use the native case-insensitive comparison function strcasecmp() to simplify the demonstration/explanation.

    Here is the plain English explanation of how each function will behave:

    1. array_diff() - case-sensitive value-only comparisons
    2. array_diff_key() - case-sensitive key-only comparisons
    3. array_diff_ukey() - case-insensitive key-only comparisons
    4. array_diff_assoc() - case-sensitive key and case-sensitive value comparisons
    5. array_diff_uassoc() - * first-occurring case-insensitive key and case-sensitive value comparisons
    6. array_udiff() - case-insensitive value-only comparisons
    7. array_udiff_assoc() - case-sensitive key and case-insensitive value comparisons
    8. array_udiff_uassoc() - case-insensitive key and case-insensitive value comparisons

    *Pay very close attention to how 5. array_diff_uassoc() behaves differently with respect to the ['c' => 'd'] and ['m' => 'n'] comparisons due to the order of qualifying occurences in the $second array.

    Code: (Demo)

    $first = [
        'A' => 'B',
        'c' => 'd',
        'E' => 'f',
        'g' => 'I',
        'k' => 'F',
        'm' => 'n',
        'o' => 'p',
    ];
    
    $second = [
        'a' => 'b',
        'A' => 'b',
        'C' => 'd',
        'c' => 'D',
        'e' => 'F',
        'G' => 'H',
        'i' => 'B',
        'J' => 'D',
        'm' => 'N',
        'M' => 'n',
        'O' => 'r',
    ];
    
    echo "array_diff()\n";
    var_export(array_diff($first, $second));
    echo "\n---\narray_diff_key()\n";
    var_export(array_diff_key($first, $second));
    echo "\n---\narray_diff_ukey()\n";
    var_export(array_diff_ukey($first, $second, 'strcasecmp'));
    echo "\n---\narray_diff_assoc()\n";
    var_export(array_diff_assoc($first, $second));
    echo "\n---\narray_diff_uassoc()\n";
    var_export(array_diff_uassoc($first, $second, 'strcasecmp'));
    echo "\n---\narray_udiff()\n";
    var_export(array_udiff($first, $second, 'strcasecmp'));
    echo "\n---\narray_udiff_assoc()\n";
    var_export(array_udiff_assoc($first, $second, 'strcasecmp'));
    echo "\n---\narray_udiff_uassoc()\n";
    var_export(array_udiff_uassoc($first, $second, 'strcasecmp', 'strcasecmp'));
    

    Output:

    array_diff()
    array (
      'E' => 'f',
      'G' => 'I',
      'o' => 'p',
    )
    ---
    array_diff_key()
    array (
      'E' => 'f',
      'k' => 'F',
      'o' => 'p',
    )
    ---
    array_diff_ukey()
    array (
      'k' => 'F',
    )
    ---
    array_diff_assoc()
    array (
      'A' => 'B',
      'c' => 'd',
      'E' => 'f',
      'G' => 'I',
      'k' => 'F',
      'm' => 'n',
      'o' => 'p',
    )
    ---
    array_diff_uassoc()
    array (
      'A' => 'B',
      'E' => 'f',
      'G' => 'I',
      'k' => 'F',
      'm' => 'n',
      'o' => 'p',
    )
    ---
    array_udiff()
    array (
      'G' => 'I',
      'o' => 'p',
    )
    ---
    array_udiff_assoc()
    array (
      'E' => 'f',
      'G' => 'I',
      'k' => 'F',
      'o' => 'p',
    )
    ---
    array_udiff_uassoc()
    array (
      'G' => 'I',
      'k' => 'F',
      'o' => 'p',
    )