0

I want to merge an array to another sub array. The code as below:

<?php

$data = array(
    'id' => array(),
    'data' => array(
        'rows' => array(
            array('name'=>'abc123'),
            array('name'=>'abc456'),
            array('name'=>'abc789'),        
        )   
    )
);


$temp = array(
   array('name'=>'def123'),
   array('name'=>'def456'),
   array('name'=>'def789')      
);

$data['data']['rows'] += $temp;

var_dump($data);

However it didn't work. I also try with array_merge but it is still the same. The only solution I can come up is using for-loop, but I don't want to use an addition for-loop.

1
  • I try to add data in $temp to 'rows' Commented Nov 26, 2013 at 13:41

2 Answers 2

2

Merging the arrays will work with array_merge(), but you have to remember to use the return-value of the function:

$data['data']['rows'] = array_merge($data['data']['rows'], $temp);

Codepad Example

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, It is what I need. :|
1

Try

$data['data']['rows'] = array_merge($data['data']['rows'], $temp);

Demo.

Comments

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.