1

If I have two arrays and I want to "combine" them into one array whilst maintaining each individual array and leaving the keys as they are as they are always duplicated each iteration, can I do this?:

$array1 = array('0'=>'Bob', '1'=>'Tom', '2'=>'John');
$array2 = array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan');

If I use array_merge:

$new_array = array_merge($array1, $array2);

I get:

array('0'=>'Bob','1'=>'Tom','2'=>'John','3'=>'Michelle','4'=>'Joan','5'=>'Susan')

whereas I want to get something like:

array(array('0'=>'Bob', '1'=>'Tom', '2'=>'John'),array('0'=>'Michelle', '1'=>'Joan', '2'=>'Susan'))

2 Answers 2

3

Create a new array and add the other arrays to that one like this:

$arr = array($array1, $array2);

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

Comments

1

$ new_array = array ($ array, $ array2);

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.