2012-02-11 26 views
13

我有兩個數據對象從JSON轉換而來。兩者都非常複雜,我想以類似的方式將它們合併到jQuery如何使用擴展合併兩個對象。合併PHP中的兩個複雜對象

例如

JSON 1:

{ 
    ... 
    "blah": 
    { 
     "params": 
     { 
      "foo": 
      { 
       "default": "bar", 
       "misc": "0", 
       ... 
      }, 
      ... 
     }, 
     ... 
    }, 
    ... 
} 

JSON 2:

{ 
    ... 
    "blah": 
    { 
     "params": 
     { 
      "foo": 
      { 
       "value": "val", 
       "misc": "1", 
       ... 
      }, 
      ... 
     }, 
     ... 
    }, 
    ... 
} 

合併到

{ 
    ... 
    "blah": 
    { 
     "params": 
     { 
      "foo": 
      { 
       "default": "bar", 
       "value": "val", 
       "misc": "1", 
       ... 
      }, 
      ... 
     }, 
     ... 
    }, 
    ... 
} 

什麼是與PHP對象接近這一目標的最佳途徑。

+4

http://www.php.net/manual/en/function.array-merge-recursive.php – 2012-02-11 16:28:36

回答

31

解碼每個JSON字符串的關聯數組,合併結果和重新編碼

$a1 = json_decode($json1, true); 
$a2 = json_decode($json2, true); 

$res = array_merge_recursive($a1, $a2); 

$resJson = json_encode($res); 

更新: 如果你有一個具體的合併要求,那麼你就需要編寫自己的合併功能。 我寫了一個滿足您的要求,如問題中所述。 如果您有其他未提及的要求,則可能需要對其進行調整。

<?php 

$json1 = ' 
{ 
    "blah": 
    { 
     "params": 
     { 
      "foo": 
      { 
       "default": "bar", 
       "misc": "0" 
      } 
     }, 
     "lost": 
     { 
      "one": "hat", 
      "two": "cat" 
     } 
    } 
}'; 

$json2 = ' 
{ 
    "blah": 
    { 
     "lost": "gone", 
     "params": 
     { 
      "foo": 
      { 
       "value": "val", 
       "misc": "1" 
      } 
     } 
    }, 
    "num_array": [12, 52, 38] 
}'; 


$a1 = json_decode($json1, true); 
$a2 = json_decode($json2, true); 


/* 
* Recursive function that merges two associative arrays 
* - Unlike array_merge_recursive, a differing value for a key 
* overwrites that key rather than creating an array with both values 
* - A scalar value will overwrite an array value 
*/ 
function my_merge($arr1, $arr2) 
{ 
    $keys = array_keys($arr2); 
    foreach($keys as $key) { 
     if(isset($arr1[$key]) 
      && is_array($arr1[$key]) 
      && is_array($arr2[$key]) 
     ) { 
      $arr1[$key] = my_merge($arr1[$key], $arr2[$key]); 
     } else { 
      $arr1[$key] = $arr2[$key]; 
     } 
    } 
    return $arr1; 
} 


$a3 = my_merge($a1, $a2); 
$json3 = json_encode($a3); 
echo($json3); 

/* 
{ 
    "blah": 
    { 
     "params": 
     { 
      "foo": 
      { 
       "default": "bar", 
       "misc": "1", 
       "value": "val" 
      } 
     }, 
     "lost": "gone" 
    }, 
    "num_array": [12,52,38] 
} 
*/ 
+0

這幾乎是這樣做的,但不是覆蓋現有的鍵,而是取兩個值並創建一個數組包含這兩個。有沒有辦法改變這種行爲? – 9point6 2012-02-11 23:34:21

+1

我已經更新了我的答案 - 合併的目的是爲您的需求而建立 – meouw 2012-02-13 12:44:08

+0

甜,這與我最後所做的非常接近。謝謝! – 9point6 2012-02-19 12:21:32

3

使用array-replace-recursive()http://www.php.net/manual/en/function.array-replace-recursive.php
它確實正是你想要的。

+2

歡迎來到StackOverflow!你能提供一個代碼示例嗎?在這裏不接受鏈接的答案。 – 2013-02-06 14:29:29

+6

Hello @JanDvorak鏈接下有足夠多的代碼示例。如果您覺得您還需要更多的幫助,請提供更好的答案。 – Infinum 2013-02-08 16:56:39

+0

對不起,但這個答案是錯誤的 - 函數是爲數組 - 不是對象。問題中的數據包含對象。當然,你可以將對象轉換爲數組,否則,結果不會再有相同的對象結構。 – Gerfried 2016-11-30 13:07:37