Advertisement
Guest User

Untitled

a guest
Jan 10th, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.11 KB | None | 0 0
  1. <?php
  2.  
  3. // I have two near-identical arrays, with the only current difference being value of the "language" key - the first array's value is "en", and the second is "de"
  4.  
  5. $en = array ( '_id' => '50eda8e7f2c3777514000005', 'name' => array ( 'singular' => NULL, 'plural' => NULL, ), 'fields' => array ( 'price' => array ( 'label' => 'Preis', ), ), 'project_id' => 1, 'file' => 'test', 'language' => 'en', );
  6.  
  7. $de = array ( '_id' => '50eda8e7f2c3777514000222', 'name' => array ( 'singular' => NULL, 'plural' => NULL, ), 'fields' => array ( 'price' => array ( 'label' => 'Preis', ), ), 'project_id' => 1, 'file' => 'test', 'language' => 'de', )
  8.  
  9. // So, I want to change, delete or add ANY key in the $en array, and it must also then change in the $de array. So if I ever "price" changes in the $en array to "New Price", then this exact key must also change in the $de array to "New Price". If I delete the "plural" key in $en, it must also then be removed from $de. If I add a new key "new_key" with "new_value" as it's value in the $en array, it must also then be added correctly in the $de array.
  10.  
  11. // I only have direct access to changing the $en array myself. All changes to this must then propagate out to all other language arrays ($de is the first one, there may be $it, $fr etc...)
  12.  
  13. // These are all stored in MongoDB, but ONLY the $en array will ever be changed manually. This is why I need a coded function to perform this propagation to other language arrays automatically.
  14.  
  15. // Here is what I have so far: I need to change the "price" key to "New Price" in the $en array, and this will therefore automagically change the $de array key to "New Price". The functions I currently have may or may not be required.
  16.  
  17. // -----------------------------------------------------------------------
  18.  
  19. var_dump('EN array');
  20. var_dump($en);
  21. var_dump('DE array');
  22. var_dump($de);
  23. echo '<hr>';
  24. // Get differences
  25. var_dump('Differences');
  26. $differences = arrayRecursiveDiff($en,$de);
  27. // Remove language because that's obvious
  28. traverseArray($differences, array('language'));
  29. var_dump($differences);
  30. echo '<hr>';
  31. // Next line is the problem - array_replace_recursive() still adds the old "price" to the output. Not even sure if this is the best way of doing it.
  32. var_dump(array_replace_recursive($en,$de,$differences['additions']));
  33.  
  34.  
  35.  
  36. // ---- FUNCTIONS ----
  37. /**
  38.  * Returns the difference between two N dimensional arrays as
  39.  * additions, changes and deletions
  40.  *
  41.  * @param array $aArray1 Array to use for the search (small)
  42.  * @param array $aArray2 Array to be searched through (large)
  43.  * @return array array(additions,changes,deletions)
  44.  */
  45. function arrayRecursiveDiff(array $aArray1, array $aArray2) {
  46.  
  47.     $aReturn = array("additions"=>array(), "changes"=>array(), "deletions"=>array());
  48.  
  49.     foreach ($aArray1 as $mKey => $mValue)
  50.     {
  51.     if (array_key_exists($mKey, $aArray2))
  52.     {
  53.         if (is_array($mValue))
  54.         {
  55.         $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
  56.  
  57.         foreach($aRecursiveDiff as $listKey=>$list)
  58.         {
  59.           if (count($list))
  60.           {
  61.             $aReturn[$listKey][$mKey] = $list;       
  62.           }
  63.         }
  64.         }
  65.         else
  66.         {
  67.         if ($mValue != $aArray2[$mKey])
  68.         {
  69.             if(($mValue == null || $mValue == "") && is_array($aArray2[$mKey]))
  70.             {
  71.             $aReturn["deletions"][$mKey] = $aArray2[$mKey];
  72.             }
  73.             else
  74.             {
  75.               $aReturn["changes"][$mKey] = $mValue;
  76.             }
  77.         }
  78.         }
  79.     }
  80.     else
  81.     {
  82.         $aReturn["additions"][$mKey] = $mValue;
  83.     }
  84.     }
  85.  
  86.     foreach ($aArray2 as $mKey => $mValue)
  87.     {
  88.     if (!array_key_exists($mKey, $aArray1))
  89.     {
  90.           $aReturn["deletions"][$mKey] = $mValue;
  91.     }
  92.     }
  93.    
  94.     return $aReturn;
  95. }
  96.  
  97. // Iterates array and removes "language" key as we don't need it
  98. function traverseArray(&$array, $keys) {
  99.   foreach ($array as $key => &$value) {
  100.     if (is_array($value)) {
  101.       traverseArray($value, $keys);
  102.     } else {
  103.       if (in_array($key, $keys)){
  104.     // Found the element
  105.     // Make sure array value = two chars
  106.     // Here we could iterate $config langs
  107.     if(strlen($value) == 2) {
  108.             unset($array[$key]);
  109.     }
  110.       }
  111.     }
  112.   }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement