2015-11-25 165 views
1

另一多陣列的特定元件I具有第一陣列看起來像這樣:合併數組在PHP

Array 
(
[0] => Array 
    (
     [data] => Array 
      (
       [id] => 1 
       [type] => asset 
       [description] => Real Estate 
       [value] => 350000 
      ) 
    ) 
) 

第二陣列看起來像這樣:

Array 
(
[0] => Array 
    (
     [owners] => Array 
      (
       [data] => Array 
        (
         [id] => 1 
         [percentage] => 100 
        ) 
      ) 
    ) 
) 

我需要插入第二數組第一個在「數據」的水平,所以它看起來是這樣的:

Array 
(
[0] => Array 
    (
     [data] => Array 
      (
       [id] => 1 
       [type] => asset 
       [description] => Real Estate 
       [value] => 350000 
       [owners] => Array 
       (
        [data] => Array 
         (
          [id] => 1 
          [percentage] => 100 
         ) 
       ) 
      ) 
    ) 
) 

我試過array_merge,但出來放不正如我所料。首先將第二個數組的正常追加添加到第一個數組的範圍之外。

任何人都可以建議我將如何在上面顯示的級別添加第二個? THX

+4

請出示相關的代碼,這將使它更容易確定是什麼問題。 – plamut

+0

'$ array1 [0] ['data'] ['owners'] = $ array2 [0] ['owners']' – spinkus

+0

$ array1 [0] ['data'] ['owners'] = $ array2 [0 ]['擁有者'] –

回答

2
<?php 
    $array1 = Array("0" => Array("data" => Array("id" => "1", "type" => "asset", "description" => "Real Estate", "value" => "350000"))); 

    $array2 = Array("0" => Array ("owners" => Array("data" => Array("id" => "1", "percentage" => "100")))); 

    // try this 
    $array1[0]['data']['owners'] = $array2[0]['owners']; 

    echo "<pre>"; print_r($array1); 
?> 

這會給輸出

Array 
(
    [0] => Array 
     (
      [data] => Array 
       (
        [id] => 1 
        [type] => asset 
        [description] => Real Estate 
        [value] => 350000 
        [owners] => Array 
         (
          [data] => Array 
           (
            [id] => 1 
            [percentage] => 100 
           ) 

         ) 

       ) 

     ) 

) 

See Working link