Question

I will like to inquire if there are any tools out there specifically in php that could do something like this comparing two json files for differences and update the new version with the old one ? btw, i tried php's array_diff* and they only give differences in one dimension. If there is someone out there who already did something similar, i will appreciate some pointers.

Was it helpful?

Solution

Have you tried to use a recursive array_diff?

function arrayRecursiveDiff($aArray1, $aArray2) {
  $aReturn = array();

  foreach ($aArray1 as $mKey => $mValue) {
    if (array_key_exists($mKey, $aArray2)) {
      if (is_array($mValue)) {
        $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
        if (count($aRecursiveDiff)) { $aReturn[$mKey] = $aRecursiveDiff; }
      } else {
        if ($mValue != $aArray2[$mKey]) {
          $aReturn[$mKey] = $mValue;
        }
      }
    } else {
      $aReturn[$mKey] = $mValue;
    }
  }
  return $aReturn;
} 

the function is taken from here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top