Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding array two other array PHP

Tags:

arrays

php

I have two array() My first 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
)

So my question is how to insert my second array to my first array. So the result become like this :

[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
            )

I have try array_merge but the result doesn't like what i desire. When i use array_merge i get this result

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
)

here is my PHP

foreach ($xml->HeaderTemp as $HeaderTempnya)
        {   
            $HeaderTemp[] = $HeaderTempnya;
        }
like image 911
YVS1102 Avatar asked Jul 06 '26 17:07

YVS1102


2 Answers

@bfahmi it almost working

Try to change it like this

foreach($first_array as $key => $value){
  $first_array[$key]->session_id  = $second_array['session_id'];
}
like image 176
Boby Avatar answered Jul 09 '26 07:07

Boby


foreach($first_array as $key => $value){
  $first_array[$key]['session_id']  = $second_array['session_id'];
}
like image 29
Fahmi B. Avatar answered Jul 09 '26 08:07

Fahmi B.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!