2

let's say I have two arrays like so:

$array1 = array('A' => array(
        'B' => array(
            'C' => array(
                'D' => array(
                    'data' => array(
                        0 => array(
                            'id' => 1,
                            'name' => 'name 1'),
                        1 => array(
                            'id' => 2,
                            'name' => 'name 2')))))));

$array2 = array('A' => array(
        'B' => array(
            'C' => array(
                'E' => array(
                    'data' => array(
                        0 => array(
                            'id' => 3,
                            'name' => 'name 3'),
                        1 => array(
                            'id' => 4,
                            'name' => 'name 4')))))));

As you can see, the two arrays have the same key A, B, and C but the keys are different afterwards. How do I merge these two arrays into something like this:

$final_array = array('A' => array(
                'B' => array(
                    'C' => array(
                        'D' => array(
                            'data' => array(
                                0 => array(
                                    'id' => 1,
                                    'name' => 'name 1'),
                                1 => array(
                                    'id' => 2,
                                    'name' => 'name 2'))), 
                        'E' => array(
                            'data' => array(
                                0 => array(
                                    'id' => 3,
                                    'name' => 'name 3'),
                                1 => array(
                                    'id' => 4,
                                    'name' => 'name 4')))))));

As you can see, in this case I merge the arrays together into the same array that contains different keys for both. In order words, here I'm putting the array starting from key E from the second array into the array with index C.

Any help will be appreciated, thanks

EDIT: Now, how about if my arrays ($array1, $array2, $array3, $array4, etc...) are generated inside a foreach loop, how do I merge all of those arrays together (Notice that I do not know the number of arrays beforehand)

2 Answers 2

6

http://php.net/manual/en/function.array-merge-recursive.php

print_r(array_merge_recursive($array1, $array2));

This should do the trick.

Added:

$collection=array();
foreach() {
     $collection[]=$myArray; //here you add your array to collection
} 
print_r(call_user_func_array('array_merge_recursive', $collection));
Sign up to request clarification or add additional context in comments.

5 Comments

Don't forget to tick my answer if you have no more questions. :) Glad to help.
actually I have another question (I knew my question was more challenging than that ;), See edit.
It works, but the only problem is that you are limiting $collection to 4 arrays, like I said, $collection could contain 10, or 15 or 20 arrays and those arrays are retrieved inside a foreach based on some condition. Anyway, it's easy to modify it now to get what I want, thanks again a million times man
Think again. If your arrays are generated in foreach loop, you can store them in $collection. Updated.
Almost 9 years later this answer saves me.
-1

i have not tested this but try this code:

foreach( $array1 as $key => $val )
{
 if( !in_array( $key, $array2 ) )
 {
  $array2[$key] = $val;
 }
}

EDIT

use Rok Kralj's answer, using native functions are probably the best way to do this as they are much faster.

1 Comment

please explain the reason for down voting?

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.