How to merge two array with same keys without loop in PHP ?
In this post we will give you information about How to merge two array with same keys without loop in PHP ?. Hear we will give you detail about How to merge two array with same keys without loop in PHP ?And how to use it also give you demo for it if it is necessary.
you want to merge two array but without overwrite value then you can see how to merge two array in following example.you have two array and you want to merge with overwrite with key then you fetch many problem. like this two array :
My Array:
$array1 = [
'0'=> ['name'=>'Hardik','Surname'=>'Savani'],
'1'=> ['name'=>'Harsukh','Surname'=>'Makawana'],
];
$array2 = [
'0'=> ['name1'=>'Harshad','Surname1'=>'Pathak'],
'1'=> ['name1'=>'Vimal','Surname1'=>'Kashiyani'],
];
AND You want to merge like that :
Output:
Array
(
[0] => Array
(
[name] => Hardik
[Surname] => Savani
[name1] => Harshad
[Surname1] => Pathak
)
[1] => Array
(
[name] => Harsukh
[Surname] => Makawana
[name1] => Vimal
[Surname1] => Kashiyani
)
)
Then you want to make this array without any loop, then you can use array_map() and array_merge(). this both function help to make this output as you want like this way to use.
Example:
$result = array_map(function($array1,$array2){
return array_merge(isset($array1) ? $array1 : array(), isset($array2) ? $array2 : array());
},$array1,$array2);
print_r($result);
Try this………
Hope this code and post will helped you for implement How to merge two array with same keys without loop in PHP ?. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs