How can you combine two arrays?

They are simplified as follows, which is sufficient for this issue. This question is based on this answer .

1

[a][b][] 

and

2

 [a][c] 

where both arrays have one common subarray [a].

I would like to have it

 [a][c][b][] 

I executed the following command unsuccessfully

 array1[a] + array2[a] 
-2
source share
6 answers
 foreach($array1 as $a => $c) { $end_array[$c] = $array2[$a]; } 

or

 // For every [a] foreach($array1 as $a => $c) { // Get the [b] $b = $array2[$a]; // Add it to [a][c] $end_array[$a][$c] = $b; // Making it $end_array[$a][$c][$b] = array(....); } 
+1
source

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

+1
source

Well, I understand what you're talking about. (For everyone else, see the Link he posted: http://dpaste.com/81464/ )

 var $output = array(); foreach ($array1 as $index => $a1) { $output[$index] = $a1; $output[$index]['title'] = $array2[$index]['title']; } 
+1
source

PHP provides you with a solution to solve this common problem:

 $a = array_merge($b, $c); 

With this solution, you take all the elements inside $ b and combine them with $ c. But if you use associative arrays, you should notice that you replace the values ​​in $ b with the numbers in $ c.

For instance:

 <?php $a = array( 'ka' => 1, 'kb' => 1, ); $b = array( 'kb' => 2, 'kc' => 2, ); print_r(array_merge($a, $b)); ?> 

The result of this code will be something like:

 Array ( [ka] => 1 [kb] => 2 [kc] => 2 ) 
0
source
 for($i = 0; $i < sizeof($array); $i++) { $mergedarray[a][b] = $a[a][b]; $mergedarray[a][c] = $b[a][c]; } 

as far as I can tell, this is what you want, so both sub-keys have the same root key.

0
source

All Articles