Free cookie consent management tool by TermsFeed Generator PHP Array Merge | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Array Merge

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

Category: programming

Tags: #php #array

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP DataTypes
Mar 21, 2023 programming

PHP DataTypes

Today we are going to learn about Data Types in PHP. Data types are another important subject in programming. The better you know the data types you are working with, the more skillful you become. ...

5 Min Read Read More
PHP Static
Mar 22, 2023 programming

PHP Static

Today we are going to talk about Static methods and properties in PHP. You can access the methods and properties of a class without create a new object but for that you need to know about static. ...

5 Min Read Read More
How to Split an Excel File into Multiple Files Using Python
May 08, 2024 programming

How to Split an Excel File into Multiple Files Using Python

Sometimes you might have an excel files that you need to split. splitting the file into multiple files with Python can be easily done. ...

7 Min Read Read More
Alpine.js Beginner to Advanced 2025: Everything You Need to Know
Jul 18, 2025 programming

Alpine.js Beginner to Advanced 2025: Everything You Need to Know

JavaScript frameworks have exploded in popularity over the past decade, but not every project needs—or benefits from—the complexity of tools like React or Vue. Sometimes, all you want is to sprinkle interactivity into your HTML without a heavy build process. This is exactly where Alpine.js shines. ...

29 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...