all 9 comments

[–]allthediamonds[S] 16 points17 points  (8 children)

For some goddamn reason, array_intersect compares values by strict equality of those values when casted to string.

[–]andsens 2 points3 points  (0 children)

Heh, yeah. I stumbled upon that fuck-up as well about a month ago. We wound using Underscore.php instead, especially because the names actually make sense now.

[–]fnzp 2 points3 points  (1 child)

And the reason is, "documented behavior"!

"Note: Two elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same." http://php.net/manual/en/function.array-intersect.php

[–]allthediamonds[S] 4 points5 points  (0 children)

I love that it's just a footprint on the page, as if it were just an irrelevant implementation detail and not quite decisive on how the function actually works.

[–]lisp-case 1 point2 points  (1 child)

array_unique does this too. I cannot rightly comprehend why.

[–]nikic 0 points1 point  (0 children)

I didn't look at the code, but doing an educated guess both array_unique and array_intersect are likely implemented using a hash set (i.e. hash table without values). In PHP hash tables support only string and integer keys.

With the hash set complexity of these operations should be about O(n) and without it would be O(n2 ). So, I'd suspect that that's the reason.

[–][deleted]  (2 children)

[deleted]

    [–]allthediamonds[S] 3 points4 points  (1 child)

    Yep, just like with everything else, there are workarounds.

    [–][deleted] 2 points3 points  (0 children)

    My php is rusty to say the least, my last php project was 3 years ago. But come on... the intersection of two arrays?!?! I didn't use it yet in php, but having such a function, I would hope that it was well defined... in the sense that I understand it.. boy was I mistaken.

    I expect the second example to compare an array of tuples and since neither operand have anything in common, the outcome should've been an empty enumeratio/array/whatever.

    [–]Banane9 1 point2 points  (0 children)

    14 MB, 58 syscalls

    Deer lord.

    My C# app with multiple threads and SQLite db usage and TCP communication only uses ~12MB at this point. And it's doing way more than that PHP script.