PHP code example of stolt / json-merge-patch

1. Go to this page and download the library: Download stolt/json-merge-patch library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stolt / json-merge-patch example snippets


 Rs\Json\Merge\Patch;

$targetDocument = json_decode('{"title":"Goodbye!","author":{"givenName":"John","familyName":"Doe"},"tags":["example","sample"],"content":"This will be unchanged"}');

$patchDocument = json_decode('{"title":"Hello!","phoneNumber":"+01-123-456-7890","author":{"familyName":null},"tags":["example"]}');

$patchedDocument = (new Patch())->apply(
    $targetDocument,
    $patchDocument
); // '{"title":"Hello!","author":{"givenName":"John"},"tags":["example"],"content":"This will be unchanged","phoneNumber":"+01-123-456-7890"}'

 Rs\Json\Merge\Patch;

$sourceDocument = json_decode('{"a":"b","b":"c"}');
$targetDocument = json_decode('{"b":"c"}');

$generatedPatchDocument = (new Patch())->generate(
    $sourceDocument,
    $targetDocument
); // '{"a":null}'

 Rs\Json\Merge\Patch;

$patchDocument1 = json_decode('{"a":"b"}');
$patchDocument2 = json_decode('{"b":"c"}');

$mergedPatchDocument = (new Patch())->merge(
    $patchDocument1,
    $patchDocument2
); // '{"a":"b","b":"c"}'