2016-07-16 48 views
3

我有兩個array()我的第一個數組:陣列添加其他兩個數組PHP

Array 
(
    [0] => SimpleXMLElement Object 
     (
      [ID] => 14212 
      [TransactionNo] => 20160712-K-DTS2-14273 
      [TransactionDate] => 2016-07-12T10:55:09.023+07:00 
      [TotalTransaction] => 14000 
      [LocationID] => 1 
      [UserID] => 1224 
      [CustomerCode] => K-DTS2 
      [SendStatus] => true 
     ) 

    [1] => SimpleXMLElement Object 
     (
      [ID] => 14213 
      [TransactionNo] => 20160712-K-DTS2-14274 
      [TransactionDate] => 2016-07-12T11:24:31.84+07:00 
      [TotalTransaction] => 12900 
      [LocationID] => 1 
      [UserID] => 1224 
      [CustomerCode] => K-DTS2 
      [SendStatus] => true 
     ) 
) 

Array 
(
    [session_id] => 16:09:15:59 
) 

所以我的問題是如何將我的第二陣列插入到我的第一個數組。所以,結果變成了這個樣子:

[0] => SimpleXMLElement Object 
      (
       [ID] => 14212 
       [TransactionNo] => 20160712-K-DTS2-14273 
       [TransactionDate] => 2016-07-12T10:55:09.023+07:00 
       [TotalTransaction] => 14000 
       [LocationID] => 1 
       [UserID] => 1224 
       [CustomerCode] => K-DTS2 
       [SendStatus] => true 
       [session_id] => 16:09:15:59 
      ) 

我有嘗試array_merge但結果不喜歡我的願望。當我使用array_merge我得到這個結果

Array 
(
    [0] => SimpleXMLElement Object 
     (
      [ID] => 2144 
      [TransactionNo] => 20160713-K-LFJBLP-02158 
      [TransactionDate] => 2016-07-13T11:32:33.6+07:00 
      [TotalTransaction] => 74900 
      [LocationID] => 1 
      [UserID] => 11418 
      [CustomerCode] => K-LFJBLP 
      [SendStatus] => true 
     ) 
     [session_id] => 16:09:19:52 
) 

這裏是我的PHP

foreach ($xml->HeaderTemp as $HeaderTempnya) 
     { 
      $HeaderTemp[] = $HeaderTempnya; 
     } 
+0

添加到兩個對象? – splash58

+0

'foreach($ array2 as $ key => $ value) $ array1 [0] - > addChild($ key,$ value);' – splash58

回答

2

@bfahmi幾乎工作

試圖改變它這樣

foreach($first_array as $key => $value){ 
    $first_array[$key]->session_id = $second_array['session_id']; 
} 
1
foreach($first_array as $key => $value){ 
    $first_array[$key]['session_id'] = $second_array['session_id']; 
} 
+0

它變成這樣[1] => SimpleXMLElement對象 ( [@attributes ] =>數組 ( [SESSION_ID] => 16:09:27:15 ) [ID] => 2161 [TransactionNo] => 20160714-K-LFJBLP-02175 [TransactionDate] => 2016- 07-14T12:46:18.81 + 07:00 [TotalTransaction] => 104500 [LocationID] => 1 [用戶名] => 339 [CustomerCode] => K-LFJBLP [SendStatus] =>真 ) – YVS1102

+0

@ YVS1102;我更正了答案$ key => $ value not($ key as $ value) – bfahmi