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/* 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
1function custom_array_merge(&$array1, &$array2) {
2 $result = Array();
3 foreach ($array1 as $key_1 => &$value_1) {
4 // if($value['name'])
5 foreach ($array2 as $key_1 => $value_2) {
6 if($value_1['name'] == $value_2['name']) {
7 $result[] = array_merge($value_1,$value_2);
8 }
9 }
10
11 }
12 return $result;
13}