添加数组另外两个数组PHP

时间:2016-07-16 07:21:42

标签: php arrays

我有两个array()我的第一个数组:

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;
        }

2 个答案:

答案 0 :(得分:2)

@bfahmi它几乎正常工作

尝试像这样更改

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

答案 1 :(得分:1)

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