merge()
Deep-merge two JSON documents. Overlay values win on conflict.
Signature
php
JsonFast::merge(
string $base,
string $overlay,
?int $output = OUTPUT_ARRAY
): mixedParameters
| Name | Type | Default | Description |
|---|---|---|---|
$base | string | — | Base JSON document |
$overlay | string | — | Document merged on top |
$output | ?int | OUTPUT_ARRAY | Output mode |
Merge rules
- Objects — keys from
$overlayreplace or add to$base; nested objects merge recursively - Arrays — typically replaced by the overlay value (not concatenated) unless your documents rely on a specific shape — test with your payload
- Scalars — overlay wins
Examples
php
$merged = JsonFast::merge(
'{"name":"Allan","settings":{"theme":"dark"}}',
'{"active":false,"settings":{"language":"en"}}'
);
/*
[
'name' => 'Allan',
'active' => false,
'settings' => ['theme' => 'dark', 'language' => 'en'],
]
*/Config defaults + user overrides:
php
$effective = JsonFast::merge(
$defaultsJson,
$userOverridesJson,
JsonFast::OUTPUT_STRING
);Performance
Large-document merge is handled in Rust. See benchmarks for throughput figures on multi-megabyte payloads.
Related
diff()— see what changed between versionsapplySchema()— schema-driven defaults
