3

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;
        }
2
  • add to both objects? Commented Jul 16, 2016 at 7:26
  • foreach($array2 as $key=>$value) $array1[0]->addChild($key, $value); Commented Jul 16, 2016 at 7:37

2 Answers 2

2

@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'];
}
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

it become like this [1] => SimpleXMLElement Object ( [@attributes] => Array ( [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; i correct the answer $key => $value not ($key as $value)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.