Voting

: eight plus one?
(Example: nine)

The Note You're Voting On

xmgr2 at protonmail dot com
7 months ago
The description is kinda ambiguous at first glance, one might think that the array_diff function returns the differences across *all* given arrays (which is what I was actually looking for, and that's why I was surprised that, in the example code, the result did not include "yellow").

However, i wrote a neat oneliner to compute the differences across all given arrays:

<?php
# Returns an array with values that are unique across all of the given arrays
function array_unique_values(...$arrays) {
return
array_keys(array_filter(array_count_values(array_merge(...$arrays)), fn($count) => $count === 1));
}

# Example:
$array1 = ['a', 'b', 'c', 'd'];
$array2 = ['a', 'b', 'x', 'y'];
$array3 = ['a', '1', 'y', 'z'];

print_r(array_unique_values($array1, $array2, $array3));
?>

Result:
Array
(
[0] => c
[1] => d
[2] => x
[3] => 1
[4] => z
)

<< Back to user notes page

To Top