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]
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}
1Input : $a1=array("a"=>"raj", "b"=>"striver");
2 $a2=array("z"=>"geeks", "b"=>"articles");
3Output :
4Array
5(
6 [a] => raj
7 [b] => Array
8 (
9 [0] => striver
10 [1] => articles
11 )
12
13 [z] => geeks
14)