1
2<?php
3$array1 = array("color" => "red", 2, 4);
4$array2 = array("a", "b", "color" => "green", "shape" => "trapezoid", 4);
5$result = array_merge($array1, $array2);
6print_r($result);
7?>
8Array
9(
10 [color] => green
11 [0] => 2
12 [1] => 4
13 [2] => a
14 [3] => b
15 [shape] => trapezoid
16 [4] => 4
17)
18
1<?php
2 $array1 = [
3 "color" => "green"
4 ];
5 $array2 = [
6 "color" => "red",
7 "color" => "blue"
8 ];
9 $result = array_merge($array1, $array2);
10?>
11
12// $result
13[
14 "color" => "green"
15 "color" => "red",
16 "color" => "blue"
17]
1<?php
2
3$array1 = array('key1' => 'test1', 'key2' => 'test2');
4$array2 = array('key3' => 'test3', 'key4' => 'test4');
5
6$resultArray = array_merge($array1, $array2);
7
8// If you have numeric or numeric like keys, array_merge will
9// reset the keys to 0 and start numbering from there
10
11$resultArray = $array1 + $array2;
12
13// Using the addition operator will allow you to preserve your keys,
14// however any duplicate keys will be ignored.
1/* Array merge is basically use to merge the two array data. */
2
3<?php
4$a1=array("red","green");
5$a2=array("blue","green","yellow");
6print_r(array_merge($a1,$a2));
7?>
8
9/*
10Output:
11Array ( [0] => red [1] => green [2] => blue [3] => green [4] => yellow )
12*/
13
14<?php
15$a1=array("a"=>"red","b"=>"green");
16$a2=array("c"=>"blue","b"=>"yellow");
17print_r(array_merge($a1,$a2));
18?>
19
20/*
21Output:
22Array ( [a] => red [b] => yellow [c] => blue )
23*/
24
25/* In above example you can check the difference in output
26it takes all values of both array in final output, but not in associative array you can check.
27because one value gets overwritten by same key reference in both array.
28*/
29
1<?php
2$a1=array("red","green");
3$a2=array("blue","yellow");
4print_r(array_merge($a1,$a2));
5?>