سؤال

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.

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top