Skip to content

Core\Arr::difference

Core\Arr::difference(
    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 does not have.

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 a second list is missing and which keys are new. The third uses a comparison that treats upper and lower case as the same.

Parameters

  • $aarray<T>The array whose entries are kept.
  • $barray<T>The entries of $a that are also in this array are removed.

Options

  • onCore\Arr::SetOnoptionalWhich part of an entry is compared: Core\Arr::SetOn::Values (the default), Core\Arr::SetOn::Keys or Core\Arr::SetOn::Both.
  • bycallableoptionalCalled once per entry as ($value, $key), and may declare fewer parameters. Its result replaces the part on selected. Under Core\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 $a is 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 returned NaN.

Changelog

VersionChange
0.0.6Core\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 the second list does not have

<?nvs
// Shows how to find the values of one list that a second list does not have.
array<string> $requested = ['pen', 'paper', 'ink', 'ruler'];
array<string> $inStock = ['paper', 'ruler'];
// "pen" and "ink" are left. Each one keeps the key it had in the first list,
// so the keys here are 0 and 2.
array<string> $missing = Core\Arr::difference($requested, $inStock);
echo Core\Json::encode($missing), "\n";
Output
{"0":"pen","2":"ink"}

Compare the keys instead

<?nvs
// Shows how to find the keys one array has and another does not. The `on`
// option says which part of an entry is compared.
array<string> $fromFile = ['theme' => 'dark', 'language' => 'en', 'debug' => 'on'];
array<string> $known = ['theme' => 'light', 'language' => 'de'];
// Only `debug` is left, because it is the one key the second array does not
// have. The values are not compared at all here.
array<string> $unknown = Core\Arr::difference($fromFile, $known, { on: Core\Arr::SetOn::Keys });
echo Core\Json::encode($unknown), "\n";
Output
{"debug":"on"}

Which addresses are new

<?nvs
// Shows how to find the addresses that are new to a mailing list, while
// treating "Ada@Example.com" and "ada@example.com" as one address.
array<string> $uploaded = ['Ada@Example.com', 'grace@example.com', 'Alan@example.com'];
array<string> $subscribed = ['ada@example.com', 'alan@example.com'];
// `by` changes what is compared, not what you get back. The addresses are
// compared in lower case, and only "grace@example.com" is new.
array<string> $new = Core\Arr::difference($uploaded, $subscribed, {
by: fn(string $address): string => Core\Str::lower($address),
});
echo Core\Json::encode($new), "\n";
Output
{"1":"grace@example.com"}