Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // 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"
- $en = array ( '_id' => '50eda8e7f2c3777514000005', 'name' => array ( 'singular' => NULL, 'plural' => NULL, ), 'fields' => array ( 'price' => array ( 'label' => 'Preis', ), ), 'project_id' => 1, 'file' => 'test', 'language' => 'en', );
- $de = array ( '_id' => '50eda8e7f2c3777514000222', 'name' => array ( 'singular' => NULL, 'plural' => NULL, ), 'fields' => array ( 'price' => array ( 'label' => 'Preis', ), ), 'project_id' => 1, 'file' => 'test', 'language' => 'de', )
- // 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.
- // 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...)
- // 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.
- // 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.
- // -----------------------------------------------------------------------
- var_dump('EN array');
- var_dump($en);
- var_dump('DE array');
- var_dump($de);
- echo '<hr>';
- // Get differences
- var_dump('Differences');
- $differences = arrayRecursiveDiff($en,$de);
- // Remove language because that's obvious
- traverseArray($differences, array('language'));
- var_dump($differences);
- echo '<hr>';
- // 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.
- var_dump(array_replace_recursive($en,$de,$differences['additions']));
- // ---- FUNCTIONS ----
- /**
- * Returns the difference between two N dimensional arrays as
- * additions, changes and deletions
- *
- * @param array $aArray1 Array to use for the search (small)
- * @param array $aArray2 Array to be searched through (large)
- * @return array array(additions,changes,deletions)
- */
- function arrayRecursiveDiff(array $aArray1, array $aArray2) {
- $aReturn = array("additions"=>array(), "changes"=>array(), "deletions"=>array());
- foreach ($aArray1 as $mKey => $mValue)
- {
- if (array_key_exists($mKey, $aArray2))
- {
- if (is_array($mValue))
- {
- $aRecursiveDiff = arrayRecursiveDiff($mValue, $aArray2[$mKey]);
- foreach($aRecursiveDiff as $listKey=>$list)
- {
- if (count($list))
- {
- $aReturn[$listKey][$mKey] = $list;
- }
- }
- }
- else
- {
- if ($mValue != $aArray2[$mKey])
- {
- if(($mValue == null || $mValue == "") && is_array($aArray2[$mKey]))
- {
- $aReturn["deletions"][$mKey] = $aArray2[$mKey];
- }
- else
- {
- $aReturn["changes"][$mKey] = $mValue;
- }
- }
- }
- }
- else
- {
- $aReturn["additions"][$mKey] = $mValue;
- }
- }
- foreach ($aArray2 as $mKey => $mValue)
- {
- if (!array_key_exists($mKey, $aArray1))
- {
- $aReturn["deletions"][$mKey] = $mValue;
- }
- }
- return $aReturn;
- }
- // Iterates array and removes "language" key as we don't need it
- function traverseArray(&$array, $keys) {
- foreach ($array as $key => &$value) {
- if (is_array($value)) {
- traverseArray($value, $keys);
- } else {
- if (in_array($key, $keys)){
- // Found the element
- // Make sure array value = two chars
- // Here we could iterate $config langs
- if(strlen($value) == 2) {
- unset($array[$key]);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement