Skip to content

Core\Arr::underlay

Core\Arr::underlay(array<T> $base, array<U> ...$layers): array<T|U>Core\Arr::underlay(
    array<T> $base,
    array<U> ...$layers,
): array<T|U>

Writes one array under another, so a key that is in both keeps the value from the left.

You give a base array and as many layers as you like. Each layer is written under the result of the ones before it. A key the base already has is left alone, with the value and the position it had. A key that is new is added at the end, in the order the layers give it. Every key is treated the same way, whether it is a number or a string. Adding two arrays with + does not compile.

Good to know: this is Core\Arr::overlay with the two sides swapped. Use underlay when the values you already have must win, and overlay when the new values must win.

Parameters

  • $basearray<T>The array on top.
  • ...$layersarray<U>variadicThe arrays to add under it, in order. The value of an earlier layer is kept over the value of a later one.

Return value

Returns array<T|U>. $base's entries in their own positions, then each layer's keys not yet present, appended in order.

Examples

Defaults under what a user chose

<?nvs
// Shows how to put the default settings under the settings a user chose.
// Where a key is in both arrays, the value on the left wins.
array<string> $chosen = ['theme' => 'dark', 'timezone' => 'UTC'];
array<string> $defaults = ['theme' => 'light', 'language' => 'en', 'perPage' => '20'];
// `theme` is in both, so the chosen value stays. `language` and `perPage` are
// new, so they are added at the end.
array<string> $settings = Core\Arr::underlay($chosen, $defaults);
echo Core\Json::encode($settings), "\n";
// The two arrays you started with are unchanged.
echo Core\Json::encode($chosen), "\n";
Output
{"theme":"dark","timezone":"UTC","language":"en","perPage":"20"}
{"theme":"dark","timezone":"UTC"}

Several layers in one call

<?nvs
// Shows how to put several arrays under one another in a single call.
// An earlier layer wins over a later one.
array<string> $request = ['region' => 'eu-west'];
array<string> $project = ['region' => 'us-east', 'bucket' => 'reports'];
array<string> $company = ['region' => 'eu-central', 'bucket' => 'shared', 'retentionDays' => '30'];
// `region` comes from the request and `bucket` from the project. Only the
// last array has `retentionDays`, so that is where it comes from.
array<string> $config = Core\Arr::underlay($request, $project, $company);
foreach ($config as string $key => string $value) {
echo $key, ': ', $value, "\n";
}
Output
region: eu-west
bucket: reports
retentionDays: 30

Add the headers a request is missing

<?nvs
// Shows how to add the headers your program always sends, while keeping the
// ones the caller already set.
array<string> $sent = ['Accept' => 'text/plain', 'X-Request-Id' => '8f21c0'];
array<string> $always = [
'Accept' => 'application/json',
'Accept-Encoding' => 'gzip',
'User-Agent' => 'reports/1.4',
];
// The caller set `Accept`, so it keeps the value "text/plain". The other two
// headers are added at the end.
array<string> $headers = Core\Arr::underlay($sent, $always);
foreach ($headers as string $name => string $value) {
echo $name, ': ', $value, "\n";
}
Output
Accept: text/plain
X-Request-Id: 8f21c0
Accept-Encoding: gzip
User-Agent: reports/1.4