Skip to content

Core\Arr::SetOn

Which part of an entry is compared when you ask what two arrays have in common, or what only one of them has.

Core\Arr::SetOn is declared in the class Core\Arr. Core\Arr::difference and Core\Arr::intersection take it as the on option, and compare the values by default. Values compares values alone, Keys compares keys alone, and Both counts an entry as matching only when the key and the value both match.

The result always keeps the keys of the first array and their order.

Good to know: the by option changes the part that on selects before it is compared. Under Keys, the by function changes the key, not the value.

Examples

Comparing values names or both

<?nvs
// Two lists, and three different questions you can ask about them. What
// changes between them is which part of an entry is compared.
array<int> $mine = ['x' => 1, 'y' => 2, 'z' => 3];
array<int> $theirs = ['p' => 2, 'z' => 9];
// The values alone, which is what you get when you say nothing. The value 2 is
// in both lists, so it is what they share.
echo 'values: ', Core\Json::encode(Core\Arr::intersection($mine, $theirs)), "\n";
// The names alone. Both lists have a `z`, whatever is stored under it.
echo 'names: ', Core\Json::encode(Core\Arr::intersection($mine, $theirs, { on: Core\Arr::SetOn::Keys })), "\n";
// The name and the value together. Nothing matches on both, so the answer is
// empty.
echo 'both: ', Core\Json::encode(Core\Arr::intersection($mine, $theirs, { on: Core\Arr::SetOn::Both })), "\n";
Output
values: {"y":2}
names: {"z":3}
both: []

What only one list has

<?nvs
// `Core\Arr::difference` returns the entries of the first array that are not in the second.
// The `on` option says what to compare: values, keys, or both.
// The result keeps the keys and the order of the first array.
array<int> $mine = ['x' => 1, 'y' => 2, 'z' => 3];
array<int> $theirs = ['p' => 2, 'z' => 9];
echo 'values: ', Core\Json::encode(Core\Arr::difference($mine, $theirs)), "\n";
echo 'names: ', Core\Json::encode(Core\Arr::difference($mine, $theirs, { on: Core\Arr::SetOn::Keys })), "\n";
echo 'both: ', Core\Json::encode(Core\Arr::difference($mine, $theirs, { on: Core\Arr::SetOn::Both })), "\n";
// With `Both`, the result has every entry, because no entry has the same key and value in both.
// `z` is in both arrays, but its value is `3` in one and `9` in the other.
Output
values: {"x":1,"z":3}
names: {"x":1,"y":2}
both: {"x":1,"y":2,"z":3}

What this customer changed

<?nvs
// This program compares a customer's saved settings with the default settings.
// Each comparison uses a different `on` option.
array<string> $defaults = [
'currency' => 'EUR',
'language' => 'en',
'theme' => 'light',
'timezone' => 'Europe/Vienna',
];
array<string> $saved = [
'language' => 'de',
'theme' => 'light',
'signature' => 'Mit freundlichen Grüßen',
];
// `Both` gives each saved setting that is not in the defaults with the same key and value.
// `theme` has the default value, so it is not in the result.
echo 'changed: ', Core\Json::encode(Core\Arr::difference($saved, $defaults, { on: Core\Arr::SetOn::Both })), "\n";
// `Keys` gives the default settings that the customer never saved.
// The page shows the default value for these.
echo 'untouched: ', Core\Json::encode(Core\Arr::difference($defaults, $saved, { on: Core\Arr::SetOn::Keys })), "\n";
// `Keys` in the other direction gives the saved settings that are not defaults any more.
// The page no longer offers `signature`.
echo 'no longer offered: ', Core\Json::encode(Core\Arr::difference($saved, $defaults, { on: Core\Arr::SetOn::Keys })), "\n";
Output
changed: {"language":"de","signature":"Mit freundlichen Grüßen"}
untouched: {"currency":"EUR","timezone":"Europe/Vienna"}
no longer offered: {"signature":"Mit freundlichen Grüßen"}