diff()
Structural diff between two JSON documents.
Signature
php
JsonFast::diff(
string $before,
string $after,
?int $output = OUTPUT_ARRAY
): mixedParameters
| Name | Type | Default | Description |
|---|---|---|---|
$before | string | — | Original JSON |
$after | string | — | Updated JSON |
$output | ?int | OUTPUT_ARRAY | Output mode |
Returns
| Field | Description |
|---|---|
added | Paths present only in $after (JSON Pointer-style keys, e.g. $.role) |
removed | Paths present only in $before |
changed | Paths in both with different values — each entry has from and to |
Example return value
php
$changes = JsonFast::diff(
'{"name":"Allan","active":true}',
'{"name":"Allan","active":false,"role":"admin"}'
);
/*
[
'added' => ['$.role' => 'admin'],
'removed' => [],
'changed' => [
'$.active' => ['from' => true, 'to' => false],
],
]
*/Examples
Audit log for config updates:
php
$changes = JsonFast::diff($previousConfig, $newConfig);
foreach ($changes['changed'] as $path => $delta) {
audit()->info("Config changed: $path", $delta);
}Detect drift between stored and live API responses:
php
$drift = JsonFast::diff($cachedResponse, $liveResponse);
$hasDrift = !empty($drift['added']) || !empty($drift['removed']) || !empty($drift['changed']);Performance
Roughly 3.3× faster than native PHP diff helpers in project benchmarks — see benchmarks.
Related
merge()— combine documentsschemaDiff()— diff schemas instead of data
