2016-07-05 31 views
-1

我有兩個數組像這樣,array_replace_recursive()在這種情況下不工作,因爲我編輯相同的更多的澄清。從PHP中合併兩個數組得到一個選擇查詢:更新

Array 
(
    [0] => stdClass Object 
     (
      [Author] => 1 
      [totalComments] => 5 
      [commentsPoints] => 900 
      [commentDateDiffpoints] => 460 
     ) 

    [1] => stdClass Object 
     (
      [Author] => 2 
      [totalComments] => 4 
      [commentsPoints] => 720 
      [commentDateDiffpoints] => 24 
     ) 
    [2] => stdClass Object 
     (
      [Author] => 3 
      [totalComments] => 4 
      [commentsPoints] => 720 
      [commentDateDiffpoints] => 24 
     ) 

    [3] => stdClass Object 
     (
      [Author] => 18 
      [totalComments] => 4 
      [commentsPoints] => 720 
      [commentDateDiffpoints] => 24 
     ) 

) 

Array 
(
    [0] => stdClass Object 
     (
      [Author] => 1 
      [totalLikesGiven] => 5 
      [likesGivenOnTopicPoints] => 36 
      [likesGivenOnReplyPoints] => 108 
      [likesGivenOnBlogPoints] => 36 
      [DateDiffTopicpoints] => 1 
      [DateDiffReplypoints] => 3 
      [DateDiffBlogpoints] => 1 
     ) 

    [1] => stdClass Object 
     (
      [Author] => 3 
      [totalLikesGiven] => 1 
      [likesGivenOnTopicPoints] => 0 
      [likesGivenOnReplyPoints] => 0 
      [likesGivenOnBlogPoints] => 36 
      [DateDiffTopicpoints] => 0 
      [DateDiffReplypoints] => 0 
      [DateDiffBlogpoints] => 1 
     ) 

) 

我喜歡這樣的結果

Array 
(
    [0] => stdClass Object 
     (
      [Author] => 1 
      [totalComments] => 5 
      [commentsPoints] => 900 
      [commentDateDiffpoints] => 460 
      [totalLikesGiven] => 5 
      [likesGivenOnTopicPoints] => 36 
      [likesGivenOnReplyPoints] => 108 
      [likesGivenOnBlogPoints] => 36 
      [DateDiffTopicpoints] => 1 
      [DateDiffReplypoints] => 3 
      [DateDiffBlogpoints] => 1 
     ) 
    [1] => stdClass Object 
     (
      [Author] => 2 
      [totalComments] => 4 
      [commentsPoints] => 720 
      [commentDateDiffpoints] => 24 
     ) 

    [2] => stdClass Object 
     (
      [Author] => 3 
      [totalComments] => 4 
      [commentsPoints] => 720 
      [commentDateDiffpoints] => 24 
      [totalLikesGiven] => 1 
      [likesGivenOnTopicPoints] => 0 
      [likesGivenOnReplyPoints] => 0 
      [likesGivenOnBlogPoints] => 36 
      [DateDiffTopicpoints] => 0 
      [DateDiffReplypoints] => 0 
      [DateDiffBlogpoints] => 1 
     ) 

    [3] => stdClass Object 
     (
      [Author] => 18 
      [totalComments] => 4 
      [commentsPoints] => 720 
      [commentDateDiffpoints] => 24 
     ) 

) 

請幫助我該怎麼辦在PHP一樣。

+0

數組中對象的提示。它極大地幫助您將數據庫中的主鍵用作陣列的關鍵。 這樣你可以更快地找到你的對象 –

回答

0

只需使用array_replace_recursive

$finalArr = array_replace_recursive($arr1,$arr2); 

有一個類似thread。還有更多這樣的問題。

+0

謝謝它只是一個更新,這是通過使用$ arr1 = json_decode(json_encode($ arr1),true)將stdClass對象轉換爲數組。 $ arr2 = json_decode(json_encode($ arr2),true);最後使用$ finalArr = array_replace_recursive($ arr1,$ arr2); – dvp

+0

當第一個和第二個數組都有唯一值時,array_replace_recursive()函數不工作,它不會添加兩個記錄。 – dvp

+0

然後在這種情況下去'array_merge_recursive' – Thamilan

-2

使用數組合並php函數。 PHP array merge function

+0

鏈接只有答案是不鼓勵的。 – 2016-07-05 05:31:12

+0

@Dagon,好的。兩個更多的缺點,你實現了你的目標 –

相關問題