Core\Arr::overlay
Core\Arr::overlay(array<T> $base, array<U> ...$layers): array<T|U>Core\Arr::overlay( array<T> $base, array<U> ...$layers, ): array<T|U>
Writes one array over another, so a key that is in both takes the value from the right.
You give a base array and as many layers as you like. Each layer is written over the result of the ones before it. A key the base already has keeps the position it had and takes the new value. A key that is new is added at the end. Every key is treated the same way, whether it is a number or a string.
Good to know: a value is replaced whole. When a key holds an array on both sides, the layer's array takes the place of the one underneath it. Core\Arr::overlayDeep is the method that combines those two arrays instead.
Parameters
$basearray<T>The array underneath....$layersarray<U>variadicThe arrays to write over it, in order. A later layer replaces the values of an earlier one.
Return value
Returns array<T|U>. Every key of every argument. An existing key keeps its position and takes the last value written under it. A new key is appended.
Examples
The settings a user changed
<?nvs// Shows how to write the settings a user changed over the default settings.// Where a key is in both arrays, the value on the right wins.
array<string> $defaults = ['theme' => 'light', 'language' => 'en', 'perPage' => '20'];array<string> $chosen = ['theme' => 'dark', 'timezone' => 'UTC'];
// `theme` is in both, so the chosen value wins. `timezone` is new, so it is// added at the end.array<string> $settings = Core\Arr::overlay($defaults, $chosen);echo Core\Json::encode($settings), "\n";
// The two arrays you started with are unchanged.echo Core\Json::encode($defaults), "\n";{"theme":"dark","language":"en","perPage":"20","timezone":"UTC"}{"theme":"light","language":"en","perPage":"20"}Several layers in one call
<?nvs// Shows how to write several layers over one base. The layers are used from// left to right, so the last one wins.
array<int> $base = ['a' => 1, 'b' => 2];array<int> $second = ['b' => 20, 'c' => 3];array<int> $third = ['c' => 30, 'a' => 10];
// `a` and `b` keep the place they had in the base. `c` is new, so it sits// after them. This prints {"a":10,"b":20,"c":30}.echo Core\Json::encode(Core\Arr::overlay($base, $second, $third)), "\n";
// A key is replaced whole. The layer's list takes the place of the base's// list, and the two are not joined.array<array<string>> $tags = ['post' => ['news', 'php']];array<array<string>> $edit = ['post' => ['novis']];echo Core\Json::encode(Core\Arr::overlay($tags, $edit)), "\n";{"a":10,"b":20,"c":30}{"post":["novis"]}Apply what a form sent
<?nvs// Shows how to apply the fields a form sent to a row read from a database.// Only the fields that were sent change.
array<string> $stored = [ 'name' => 'Ada Lovelace', 'email' => 'ada@example.com', 'city' => 'London',];
// The form sent two of the three fields.array<string> $submitted = ['email' => 'ada@analytical.example', 'city' => 'Paris'];
// The name is kept, the other two take the new values.array<string> $updated = Core\Arr::overlay($stored, $submitted);foreach ($updated as string $field => string $value) { echo $field, ': ', $value, "\n";}name: Ada Lovelaceemail: ada@analytical.examplecity: Paris