PHP Array Merge
Last Updated on Feb 14, 2023
Array Merge
This function merges all the arrays that you pass as arguments and returns an array.
How does it merge the arrays?
If the key is number(indexed array) then it will be appended to the previous array.
$array = ['pratham','amir','simon','francesco'];
$arrayB = ['pratham','amir'];
array_merge($array,$arrayB);
//Array (
// [0] => pratham
// [1] => amir
// [2] => simon
// [3] => francesco
// [4] => pratham
// [5] => amir
// )
If the key is string (associative array) then it will update the value
$people = [
'php' => 'amir',
'twitter' => 'pratham',
'docker' => 'francesco',
];
$peopleB = [
'php' => 'amir updated',
'feedhive' => 'simon',
'twitter' => 'pratham updated',
];
$mergeAssociative = array_merge($people,$peopleB);
// Array (
// [php] => amir updated
// [twitter] => pratham updated
// [docker] => francesco
// [feedhive] => simon
// )
Conclusion
Now you know about array merge function in PHP.
I recommend you to open a PHP files and try to define multiple arrays. Then try to merge those arrays with array merge function.
If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!
Key takeaways
- merging arrays in PHP with array merge function