0

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.

1 Answers1

0

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

Community
  • 1
  • 1
Abed Hawa
  • 1,372
  • 7
  • 18
  • Thanks for the reference. It works. But i was expecting to update the new json file to the old one after detecting this differences. –  Mar 30 '14 at 11:17