将数组添加到另一个数组中 PHP

3

我有两个 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
        )
)

and this

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

将两个对象相加? - splash58
foreach($array2 as $key=>$value) $array1[0]->addChild($key, $value); - splash58
2个回答

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

[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 [UserID] => 339 [CustomerCode] => K-LFJBLP [SendStatus] => true ) - YVS1102
@YVS1102;我纠正了答案,应该是$key => $value而不是($key as $value)。 - Fahmi B.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接