Trying to understand the behavior of array_uintersect

Let's continue. Why does n't array_uintersect compare the values ​​of the first array after sorting ? In my humble opinion, array_udiffthey array_uintersectshould have similar algorithms, but they are not. Why?

$compare = function($a, $b) use(&$iteration_count)
    {
    echo("$a : $b\n");
    $iteration_count++;
    return strcmp($a, $b);
    };

$a = array('a', 'b', 'c');
$b = array('x', 'y', 'z');

$iteration_count = 0;
echo "array_udiff:" . json_encode(array_udiff($a, $b, $compare)) . "\n";
echo "iterations: $iteration_count\n\n";

$iteration_count = 0;
echo "array_uintersect:" . json_encode(array_uintersect($a, $b, $compare)) . "\n";
echo "iterations: $iteration_count\n\n";

Output

b : a
c : b
y : x
z : y
a : x
a : b
b : x
b : c
c : x
array_udiff:["a","b","c"]
iterations: 9

b : a
c : b
y : x
z : y
a : x  // comparison started
b : x  // but there is no comparison to skip values
c : x
array_uintersect:[]
iterations: 7
+4
source share
3 answers

An algorithm accepted by array_intersect()friends should first assume that all values ​​of the first array are present in other arrays; during the iteration, it will remove elements that do not support this statement.

If an element is not found in one of the other arrays, the implementation can do two things:

  • , ( , diff)
  • , ( ).

PHP . , , , , . :

$a = ['a.a0', 'a.a1', 'b.a2', 'c.a3'];
$b = ['a.c0', 'd.c1'];

function cmp_val($a, $b)
{
    echo "$a <=> $b\n";
    return strcmp($a[0], $b[0]);
}

print_r(array_uintersect($a, $b, 'cmp_val'));

:

...
-- intersect starts
a.a0 <=> a.c0
a.a0 <=> a.a1 <-- match
a.a1 <=> b.a2
b.a2 <=> d.c1 <-- no match
c.a3 <=> d.c1

, , , diff; , .

+3

O (n log (n)) , O (n) .

3 , .

0

At the same time, I was right and wrong. These functions have similar algorithms.

$compare = function($a, $b) use(&$iteration_count)
    {
    echo("$a : $b\n");
    $iteration_count++;
    return strcmp($a[0], $b[0]);
    };

$a = array('a1', 'b1', 'c1');
$b = array('a2', 'b2', 'c2');

$iteration_count = 0;
echo "array_uintersect:" . json_encode(array_uintersect($a, $b, $compare)) . "\n";
echo "iterations: $iteration_count\n\n";

Output

b1 : a1 
c1 : b1
b2 : a2
c2 : b2
a1 : a2  // comparison started
a1 : b1  // it trying to skip values after it have been matched
b1 : b2
b1 : c1
c1 : c2
array_uintersect:["a1","b1","c1"]
iterations: 9
0
source

All Articles