2014-09-01 30 views
1

失去價值我有一個數組:與array_merge

{ 
    "zone1":{ 
    "foo1":"bar1", 
    "foo2":"bar2", 
    "foo3":"bar3", 
    "foo4":"bar4" 
    }, 
    "zone2":{ 
    "newfoo1":"newbar1", 
    "newfoo2":"newbar2", 
    "newfoo3":"newbar3", 
    "newfoo4":"newbar4" 
    }, 
    "zone3":{ 
    "morefoo1":"morebar1", 
    "morefoo2":"morebar2", 
    "morefoo3":"morebar3", 
    "morefoo4":"morebar4" 
    } 
} 

我想合併第二陣列使用更新的值:

{ 
    "zone1":{ 
    "foo1":"updatedbar1" 
    }, 
    "zone3":{ 
    "morefoo2":"updatedbar2", 
    "morefoo4":"updatedbar4" 
    } 
} 

我已經嘗試了很多事情,林現在使用的這是什麼PHP代碼:

$array3 = array_merge($array1, $array2); 

但這個代碼給我:

{ 
    "zone1":{ 
    "foo1":"updatedbar1" 
    }, 
    "zone2":{ 
    "newfoo1":"newbar1", 
    "newfoo2":"newbar2", 
    "newfoo3":"newbar3", 
    "newfoo4":"newbar4" 
    }, 
    "zone3":{ 
    "morefoo2":"updatedbar2", 
    "morefoo4":"updatedbar4" 
    } 
} 

我想要的只是用第二個數組上的值更新第一個數組,而不會丟失任何數據。 數組是json,它們來自json文件,但語言是PHP。

+0

什麼編程語言? – MrTux 2014-09-01 22:12:37

+2

對'array_merge'的調用顯然是PHP,但是您的數組在這裏顯示爲JSON。請澄清。 – Charles 2014-09-01 22:13:44

+0

是的,我的數組來自使用json_decode和json_encode的json文件。我的語言是PHP。 – 2014-09-01 22:17:59

回答

3

您可以使用array_replace_recursive()

$array3 = array_replace_recursive($array1, $array2); 

它與您的數據創建以下數組:

Array 
(
    [zone1] => Array 
     (
      [foo1] => updatedbar1 
      [foo2] => bar2 
      [foo3] => bar3 
      [foo4] => bar4 
     ) 

    [zone2] => Array 
     (
      [newfoo1] => newbar1 
      [newfoo2] => newbar2 
      [newfoo3] => newbar3 
      [newfoo4] => newbar4 
     ) 

    [zone3] => Array 
     (
      [morefoo1] => morebar1 
      [morefoo2] => updatedbar2 
      [morefoo3] => morebar3 
      [morefoo4] => updatedbar4 
     ) 

) 

看到它的ideone

+0

後幫了我,謝謝隊友。 – Burrito 2015-07-21 22:14:11

0

array_merge()僅合併頂層數組,並且不考慮數組內的數組。因此,重複值將被後面的數組覆蓋。

可以使用array_merge_recursive()解決這個問題:

$ar1 = array("color" => array("favorite" => "red"), 5); 
$ar2 = array(10, "color" => array("favorite" => "green", "blue")); 
$result = array_merge_recursive($ar1, $ar2); 

更多信息可以在php.net page

找到 - 編輯

上述頁面上的第一個註釋說明如何合併兩個陣列,同時使用第二個更新第一個:

<?php 
/** 
* array_merge_recursive does indeed merge arrays, but it converts values with duplicate 
* keys to arrays rather than overwriting the value in the first array with the duplicate 
* value in the second array, as array_merge does. I.e., with array_merge_recursive, 
* this happens (documented behavior): 
* 
* array_merge_recursive(array('key' => 'org value'), array('key' => 'new value')); 
*  => array('key' => array('org value', 'new value')); 
* 
* array_merge_recursive_distinct does not change the datatypes of the values in the  arrays. 
* Matching keys' values in the second array overwrite those in the first array, as is  the 
* case with array_merge, i.e.: 
* 
* array_merge_recursive_distinct(array('key' => 'org value'), array('key' => 'new value')); 
*  => array('key' => array('new value')); 
* 
* Parameters are passed by reference, though only for performance reasons. They're  not 
* altered by this function. 
* 
* @param array $array1 
* @param array $array2 
* @return array 
* @author Daniel <daniel (at) danielsmedegaardbuus (dot) dk> 
* @author Gabriel Sobrinho <gabriel (dot) sobrinho (at) gmail (dot) com> 
*/ 
function array_merge_recursive_distinct (array &$array1, array &$array2) 
{ 
    $merged = $array1; 

    foreach ($array2 as $key => &$value) 
    { 
    if (is_array ($value) && isset ($merged [$key]) && is_array ($merged [$key])) 
    { 
     $merged [$key] = array_merge_recursive_distinct ($merged [$key], $value); 
    } 
    else 
    { 
    $merged [$key] = $value; 
    } 
    } 

    return $merged; 
} 
?> 
+0

它是遞歸的,我試過了,兩個數組中的值都不會被覆蓋,相互之間會是這樣:「newfoo1」:[「newbar1」,「updatedbar1」] – 2014-09-01 22:24:39

+0

似乎是與' array_replace_recursive',明天我會做更多的測試,如果失敗,我會嘗試編輯,謝謝。 – 2014-09-01 22:47:12