PHP code example of phore / json-patch

1. Go to this page and download the library: Download phore/json-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/ */

    

phore / json-patch example snippets




use Phore\JsonPatch\{JsonPatch, JsonPatchApplier};

$original = (object) ['title' => 'Draft', 'tags' => ['php']];
$patch = JsonPatch::fromArray([
    ['op' => 'test', 'path' => '/title', 'value' => 'Draft'],
    ['op' => 'replace', 'path' => '/title', 'value' => 'Published'],
    ['op' => 'add', 'path' => '/tags/-', 'value' => 'json-patch'],
]);
$result = (new JsonPatchApplier())->apply($original, $patch);
$edited = $result->value;
// $edited: {"title":"Published","tags":["php","json-patch"]}
// $original still contains "Draft" and ["php"].

use Phore\JsonPatch\PatchApplyOptions;

$options = new PatchApplyOptions(
    allowedOperations: ['add', 'remove', 'replace', 'move', 'copy', 'test'],
    allowRootReplacement: true,
);
$result = (new JsonPatchApplier())->apply($original, $patch, $options);