Skip to content

merge()

Deep-merge two JSON documents. Overlay values win on conflict.

Signature

php
JsonFast::merge(
    string $base,
    string $overlay,
    ?int $output = OUTPUT_ARRAY
): mixed

Parameters

NameTypeDefaultDescription
$basestringBase JSON document
$overlaystringDocument merged on top
$output?intOUTPUT_ARRAYOutput mode

Merge rules

  • Objects — keys from $overlay replace 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.

Native tools, weird experiments, and practical performance work.