

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain array_merge() function in PHP.
In this article, we will learn about array_merge(), a predefined PHP array function. The array_merge() is utilized to combine at least two more arrays into a single array. This function is utilized to combine the components of at least two arrays together into a single array.
This function works to merge the elements of one or more arrays together in such a way so that the values of later array are appended to the end of the former array.
Let's test this with a simple example.
<?php $array1 = array("name" => "alex", 2 ); $array2 = array("a", "b", "department" => "accounting", "id" => 13, 4); $res = array_merge($array1, $array2); print_r($res); ?>
Output:
Array ( [name] => alex [0] => 2 [1] => a [2] => b [department] => accounting [id] => 13 [3] => 4 );
Explanation:
In the above example, we have declared two arrays and merging them to result in a single array by array_merge() function.
Note:
In the event that if the arrays have similar string keys, then the later value for that key will overwrite the previous one. But in case of the arrays contain numeric keys, the later value won't overwrite the first one, yet will be appended.
Let's understand the above concept with an example.
Example:
<?php $array1 = array("name" => "alex", 2 ); $array2 = array("a", "b", "name" => "jack", "id" => 18, 2); $result = array_merge($array1, $array2); print_r($result); ?>
Output:
Array ( [name] => jack [0] => 2 [1] => a [2] => b [id] => 18 [3] => 2 )
- Related Questions & Answers
- Explain array_intersect() function in PHP.
- Explain str_split() function in PHP
- Explain array_map() function in PHP
- Explain substr() function in PHP
- array() function in PHP
- Explain Merge Sort in Python
- Explain variable / Array scope in PowerShell function.
- Explain the merge sort technique in C language
- Explain Encapsulation in PHP.
- Explain Polymorphism in PHP.
- Explain interface in PHP.
- Explain array_diff() in PHP
- Merge Sorted Array in Python
- list merge() function in C++ STL
- Merge two arrays keeping original keys in PHP