Skip to content

Core\Arr::overlayDeep

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

Writes one array over another and combines the arrays that meet under the same key.

You give a base array and as many layers as you like, the same way Core\Arr::overlay takes them. The difference is what happens when a key holds an array on both sides. Core\Arr::overlay replaces the whole value. This method combines the two arrays one level further down, and then does the same again for every key inside them, as deep as the arrays go.

A list is always replaced whole. Two lists under one key are not joined, because nothing can say what order the result should then have.

The examples below show two settings trees combined, a list that is replaced, and three layers written over each other in one call.

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, as overlay returns them. When both values under one key are maps, they are combined the same way, and the later map does not replace the earlier one.

Examples

Two settings trees

<?nvs
// Shows how to combine two settings trees. The arrays that meet under the
// same key are combined, not replaced.
array<array<string>> $defaults = [
'db' => ['host' => 'localhost', 'port' => '5432'],
'log' => ['level' => 'info'],
];
array<array<string>> $production = [
'db' => ['host' => 'db.example.com'],
];
// The port under `db` is kept, because the layer says nothing about it.
array<array<string>> $config = Core\Arr::overlayDeep($defaults, $production);
echo Core\Json::encode($config), "\n";
// `Core\Arr::overlay` replaces the whole value under `db`, so the port is
// gone.
echo Core\Json::encode(Core\Arr::overlay($defaults, $production)), "\n";
Output
{"db":{"host":"db.example.com","port":"5432"},"log":{"level":"info"}}
{"db":{"host":"db.example.com"},"log":{"level":"info"}}

A list is replaced whole

<?nvs
// Shows what is not combined. Two lists under the same key are not joined.
// The list on the right replaces the one on the left.
array<array<string>> $base = [
'hosts' => ['a.example.com', 'b.example.com'],
'db' => ['host' => 'localhost', 'port' => '5432'],
];
array<array<string>> $layer = [
'hosts' => ['c.example.com'],
'db' => ['host' => 'db.example.com'],
];
// `hosts` holds a list, so it is replaced. `db` holds keys, so it is
// combined and keeps the port.
echo Core\Json::encode(Core\Arr::overlayDeep($base, $layer)), "\n";
Output
{"hosts":["c.example.com"],"db":{"host":"db.example.com","port":"5432"}}

The configuration a program starts with

<?nvs
// Shows how a program builds its configuration from three layers: what it
// ships with, what the machine it runs on sets, and what the command line
// asked for.
array<array<string>> $shipped = [
'db' => ['host' => 'localhost', 'port' => '5432', 'name' => 'shop'],
'log' => ['level' => 'info', 'file' => 'app.log'],
];
array<array<string>> $machine = [
'db' => ['host' => 'db.internal', 'name' => 'shop_live'],
];
array<array<string>> $flags = [
'log' => ['level' => 'debug'],
];
// Every setting nobody changed is still there.
array<array<string>> $config = Core\Arr::overlayDeep($shipped, $machine, $flags);
foreach ($config as string $section => array<string> $values) {
foreach ($values as string $key => string $value) {
echo $section, '.', $key, ' = ', $value, "\n";
}
}
Output
db.host = db.internal
db.port = 5432
db.name = shop_live
log.level = debug
log.file = app.log