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.

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

They both do the same, but udiff-assoc compares the DATA with the user supplied function, while diff-uassoc compares the INDEX with the user supplied function.

As an answer to @lonsesomeday : as indicated by the ‘u’, diff_assoc will use internal functions for all comparisons, and udiff_uassoc uses provided callbacks for index and data comparison.

http://www.php.net/manual/en/function.array-diff-uassoc.php

http://www.php.net/manual/en/function.array-udiff-assoc.php

Solution 2

Perhaps a battery of test cases will clear up any confusion between these related native functions. I’ll consistently use the native case-insensitive comparision 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',
)

Solution 3

array_udiff_assoc — Computes the difference of arrays with additional index check, compares data by a callback function
array_diff_uassoc — Computes the difference of arrays with additional index check which is performed by a user supplied callback function

So, the function differ in the place where they use the callback function. udiff_assoc uses the callback to compare elements, diff_uassoc uses the callback when comparing the indices.

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply