Core\Arr::intersection
Core\Arr::intersection(
array<T> $a,
array<T> $b,
{on?: Core\Arr::SetOn, by?: callable, comparator?: callable},
): array<T>Gives you the entries of the first array that the second array also has.
The entries you get keep their own keys, and they stay in the order the first array had. Two entries are the same when their values are identical: the same type and the same content, with no conversion. The number 3 and the text "3" are different values here.
You can change what is compared. The on option takes Core\Arr::SetOn::Values (the default), Core\Arr::SetOn::Keys or Core\Arr::SetOn::Both. The by option is a function that changes the part being compared. It changes the comparison only, never the entries you get back. The comparator option replaces the comparison itself.
The examples show three of these. They find which values two lists have in common and which fields of a form are known. The third finds which settings still have the same value.
Parameters
$aarray<T>The array whose entries are kept.$barray<T>The array an entry must also be in.
Options
onCore\Arr::SetOnoptionalWhich part of an entry is compared:Core\Arr::SetOn::Values(the default),Core\Arr::SetOn::KeysorCore\Arr::SetOn::Both.bycallableoptionalCalled once per entry as($value, $key), and may declare fewer parameters. Its result replaces the partonselected. UnderCore\Arr::SetOn::Keys, that part is the key.comparatorcallableoptionalCalled as($a, $b)on two selected parts. It returns a negative number, zero or a positive number. It replaces identity, and every entry of$ais then compared with every entry of$b.
Return value
Returns array<T>. The kept entries of $a under their own keys, in $a's order.
Errors
RuntimeErrorThe comparator returnedNaN.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\Arr::difference and Core\Arr::intersection compare two arrays. Core\Arr::groupCounts counts the elements of each group. The old names were diff, intersect and countBy. |
Examples
What both lists have
<?nvs// Shows how to find the values that two lists have in common.
array<string> $needed = ['read', 'write', 'publish'];array<string> $granted = ['read', 'publish', 'archive'];
// "read" and "publish" are in both lists. Each one keeps the key it had in the// first list, so the keys here are 0 and 2.array<string> $allowed = Core\Arr::intersection($needed, $granted);echo Core\Json::encode($allowed), "\n";{"0":"read","2":"publish"}Keep only the fields you know
<?nvs// Shows how to drop the fields a form sent that your program does not know.// The `on` option says which part of an entry is compared.
array<string> $submitted = ['name' => 'Ada', 'email' => 'ada@example.com', 'isAdmin' => 'yes'];
// Only the keys of this array are used. The values are never compared, so// empty text is enough.array<string> $known = ['name' => '', 'email' => ''];
// `name` and `email` are left, with the values the form sent. `isAdmin` is// gone, because it is not a key the second array has.array<string> $clean = Core\Arr::intersection($submitted, $known, { on: Core\Arr::SetOn::Keys });echo Core\Json::encode($clean), "\n";{"name":"Ada","email":"ada@example.com"}Which settings are unchanged
<?nvs// Shows how to find the settings a user did not change, so that a program can// save only the new values.
array<string> $saved = ['theme' => 'dark', 'language' => 'en', 'fontSize' => '14'];array<string> $submitted = ['theme' => 'dark', 'language' => 'de', 'fontSize' => '16'];
// `Core\Arr::SetOn::Both` compares the key and the value together. Only `theme`// has the same value in both arrays, so it is the one setting that is kept.array<string> $unchanged = Core\Arr::intersection($saved, $submitted, { on: Core\Arr::SetOn::Both });echo Core\Json::encode($unchanged), "\n";{"theme":"dark"}