2015-05-28 51 views
1

下面的2個數組在2015年沒有像他們應該合併的那樣合併。相反,它會像上一個例子一樣創建[2016]。Array_merge_recursive沒有正確合併(php)

代碼我使用:

$合併= array_merge_recursive($陣列,$電流);

第一陣列:($電流)

Array 
(
    [Particulier] => Array 
     (
      [Weekend] => Array 
       (
        [2015] => Array 
         (
          [06] => Array 
           (
            [0] => Array 
             (
              [id] => 111 
              [startDate] => 12-06-2015 
              [endDate] => 15-06-2015 
              [price] => 920 
             ) 
           ) 
         ) 
       ) 
     ) 
) 

第二陣列:($陣列)

Array 
(
    [Particulier] => Array 
     (
      [Weekend] => Array 
       (
        [2015] => Array 
         (
          [05] => Array 
           (
            [0] => Array 
             (
              [id] => 90 
              [startDate] => 01-05-2015 
              [endDate] => 04-05-2015 
              [price] => 920 
             ) 
            [1] => Array 
             (
              [id] => 91 
              [startDate] => 08-05-2015 
              [endDate] => 11-05-2015 
              [price] => 920 
             ) 
            [2] => Array 
             (
              [id] => 92 
              [startDate] => 15-05-2015 
              [endDate] => 18-05-2015 
              [price] => 920 
             ) 
           ) 
          [06] => Array 
           (
            [0] => Array 
             (
              [id] => 112 
              [startDate] => 19-06-2015 
              [endDate] => 22-06-2015 
              [price] => 920 
             ) 
           ) 
         ) 
       ) 
      [Midweek] => Array 
       (
        [2015] => Array 
         (
          [05] => Array 
           (
            [0] => Array 
             (
              [id] => 94 
              [startDate] => 11-05-2015 
              [endDate] => 15-05-2015 
              [price] => 1225 
             ) 
            [1] => Array 
             (
              [id] => 95 
              [startDate] => 18-05-2015 
              [endDate] => 22-05-2015 
              [price] => 1225 
             ) 
           ) 
          [06] => Array 
           (
            [0] => Array 
             (
              [id] => 118 
              [startDate] => 15-06-2015 
              [endDate] => 19-06-2015 
              [price] => 1225 
             ) 
           ) 
         ) 
       ) 
     ) 
    [Clienten en patienten] => Array 
     (
      [Weekend] => Array 
       (
        [2015] => Array 
         (
          [05] => Array 
           (
            [0] => Array 
             (
              [id] => 105 
              [startDate] => 01-05-2015 
              [endDate] => 04-05-2015 
              [price] => 645 
             ) 
           ) 
         ) 
       ) 
     ) 
) 

當前的結果:($合併)

Array 
(
    [Particulier] => Array 
     (
      [Weekend] => Array 
       (
        [2015] => Array 
         (
          [05] => Array 
           (
            [0] => Array 
             (
              [id] => 90 
              [startDate] => 01-05-2015 
              [endDate] => 04-05-2015 
              [price] => 920 
             ) 
           [1] => Array 
            (
             [id] => 91 
             [startDate] => 08-05-2015 
             [endDate] => 11-05-2015 
             [price] => 920 
            ) 

           [2] => Array 
            (
             [id] => 92 
             [startDate] => 15-05-2015 
             [endDate] => 18-05-2015 
             [price] => 920 
            ) 
          ) 
         [06] => Array 
          (
           [0] => Array 
            (
             [id] => 112 
             [startDate] => 19-06-2015 
             [endDate] => 22-06-2015 
             [price] => 920 
            ) 
          ) 
        ) 
       [2016] => Array 
        (
         [06] => Array 
          (
           [0] => Array 
            (
             [id] => 111 
             [startDate] => 12-06-2015 
             [endDate] => 15-06-2015 
             [price] => 920 
            ) 
          ) 
        ) 
      ) 

編輯:預期的結果

Array 
(
    [Particulier] => Array 
    (
     [Weekend] => Array 
     (
      [2015] => Array 
      (
       [05] => Array 
       (
        [0] => Array 
        (
         [id] => 90 
         [startDate] => 01-05-2015 
         [endDate] => 04-05-2015 
         [price] => 920 
        ) 

        [1] => Array 
        (
         [id] => 91 
         [startDate] => 08-05-2015 
         [endDate] => 11-05-2015 
         [price] => 920 
        ) 

        [2] => Array 
        (
         [id] => 92 
         [startDate] => 15-05-2015 
         [endDate] => 18-05-2015 
         [price] => 920 
        ) 

       ) 

       [06] => Array 
       (
        [0] => Array 
        (
         [id] => 112 
         [startDate] => 19-06-2015 
         [endDate] => 22-06-2015 
         [price] => 920 
        ) 
        [1] => Array 
        (
         [id] => 111 
         [startDate] => 12-06-2015 
         [endDate] => 15-06-2015 
         [price] => 920 
        ) 
       ) 
      ) 
     ) 

如果需要任何其他信息,請諮詢! 在此先感謝!

+1

預期的結果嗎? –

+0

@anantkumarsingh已添加! – Hees1989

回答

1

從你所描述的你將不得不使用關聯數組(使用像「2015」的字符串索引)而不是數字索引(如2015)。

我也建議使用唯一的密鑰,作爲事件的索引,並使用替代函數array_replace_recursive防止PHP建立任何嵌套的數組(見http://php.net/manual/en/function.array-replace-recursive.php

+0

我正在通過foreach循環創建數組。那我如何獲得一個字符串索引?而不是array_replace_recursive只覆蓋以前的記錄?這不是我想要的。我想要添加。 – Hees1989

+0

如果這是問題,請發佈生成數組的foreach循環代碼。數組替換僅覆蓋現有條目。使用唯一的密鑰,您永遠不會覆蓋您的任何條目。 – Achim

+0

嘗試過並且工作!謝謝! – Hees1989