-2

array_diff_assoc and array_diff_uassoc both do the same thing (compute difference b/w array with additinal index check) the only difference is the later one accpet a callback.

The difference is just callback, In which case you should prefer array_diff_uassoc instead of array_diff_assoc.

I want to understand that If the callback is going to do the same as below is every case then what is the use of array_diff_uassoc

function key_compare_func($a, $b)
{
    if ($a === $b) {
        return 0;
    }
    return ($a > $b)? 1:-1;
}
Daric
  • 16,229
  • 11
  • 41
  • 58

1 Answers1

3

The practical difference is that the user-defined function can be anything other than the default. You define the callback yourself.

Just because the documentation only gives a simple example doesn't mean that that is the only possibility. Here is a contrived example of a callback function you'd use tom compare elements in a multi-dimensional array:

function key_compare_func($a, $b) {
    if ($a['key']['subkey'] === $b['key']['subkey']) {
        return 0;
    }
    return ($a['key']['subkey'] > $b['key']['subkey'])? 1:-1;
}

Edit: PHP7 has spaceships!

function key_compare_func($a, $b) {
    return $a['key']['subkey'] <=> $b['key']['subkey']
}
Sammitch
  • 30,782
  • 7
  • 50
  • 77