PHP code example of blancks / fast-jsonpatch-php

1. Go to this page and download the library: Download blancks/fast-jsonpatch-php 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/ */

    

blancks / fast-jsonpatch-php example snippets


use blancks\JsonPatch\FastJsonPatch;

$document = '{"foo":"bar","baz":["qux","quux"]}';

$patch = '[
    {"op":"replace","path":"\/baz\/1","value":"boo"},
    {"op":"add","path":"\/hello","value":{"world":"wide"}},
    {"op":"remove","path":"\/foo"}
]';

$FastJsonPatch = FastJsonPatch::fromJson($document);
$FastJsonPatch->apply($patch);

var_dump($FastJsonPatch->getDocument());

  $document = ['one', 'two', 'three'];
  $FastJsonPatch = new FastJsonPatch($document);
  

  $FastJsonPatch = FastJsonPatch::fromJson('{"foo":"bar","baz":["qux","quux"]}');
  

  $patch = '[{"op":"test", "path":"/foo", "value":"bar"}]';
  $FastJsonPatch->apply($patch);
  

  $patch = '[{"op":"add","path":"/foo"}]'; // invalid because there's no "value" key 
 
  if ($FastJsonPatch->isValidPatch($patch)) {
      $FastJsonPatch->apply($patch);
  } else {
      echo "Invalid patch!";
  }
  

  $FastJsonPatch = FastJsonPatch::fromJson('{"foo":"bar","baz":["qux","quux"]}');
  echo $FastJsonPatch->read('/baz/1'); // "quux"
  

  $FastJsonPatch = FastJsonPatch::fromJson('["qux","quux"]');
  var_dump($FastJsonPatch->getDocument()); // array(2) {[0]=> string(3) "qux" [1]=> string(4) "quux"}
  

  $FastJsonPatch->registerOperation(new Add);
  
 bash
composer