Last Updated: January 04, 2018
·
7.918K
· lukascivil

difference between 2 json in php

With TreeWalker you can do to get the difference between two objects in php:
https://github.com/lukascivil/TreeWalker
live example: http://treewalker.lukascivil.com.br/

<?php
include 'pathto/TreeWalker.php';

$treewalker = new TreeWalker(array(
"debug"=>true, //true => return the execution time, false => not
"returntype"=>"jsonstring") //Returntype = ["obj","jsonstring","array"]
);

Method:

//getdiff() - this method will return the diference between struct1 and struct2

$struct1 = array("casa"=>1, "b"=>"5", "cafeina"=>array("ss"=>"ddd"), "oi"=>5);
$struct2 = array("casa"=>2, "cafeina"=>array("ss"=>"dddd"), "oi2"=>5);

$treewalker->getdiff($struct1, $struct2, false) // false -> with slashs

Output:
{
    new: {
        b: "5",
        oi: 5
    },
    removed: {
        oi2: 5
    },
    edited: {
        casa: {
          oldvalue: 2,
          newvalue: 1
        },
        cafeina/ss: {
          oldvalue: "dddd",
          newvalue: "ddd"
        }
    },
    time: 0
}