On Sat, Nov 11, 2023 at 6:05 PM Andreas Hennings <[email protected]>
wrote:
> Hello internals,
> I noticed that array functions like array_diff(), array_intersect()
> etc use weak comparison.
>
>
That's not quite correct. Using the example of array_diff, the comparison
is a strict equality check on a string cast of the values. So
array_diff([""], [false]) will indeed be empty
but array_diff(["0"],[false]) will return ["0"].
Tbh any use case for whatever array function but with strict comparison is
such an easy thing to implement in userland[1] I'm not bothered about
supporting it in core. But that's just me. I don't generally like the idea
of adding new array_* or str_* functions to the global namespace without
very good cause. There is a precedent for it though, in terms of changes
which have gone through in PHP 8, such as array_is_list or str_starts_with.
[1] Example:
function array_diff_strict(array $array1, array ...$arrays): array
{
$diff = [];
foreach ($array1 as $value) {
$found = false;
foreach ($arrays as $array) {
if (in_array($value, $array, true)) {
$found = true;
break;
}
}
if (!$found) {
$diff[] = $value;
}
}
return $diff;
}