- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
The easiest way to concatenate two arrays in PHP?
Simply use, array_merge() to concatenate two arrays in PHP. Let’s say the following are out arrays −
$nameArray1 = array('John','David'); $nameArray2 = array('Mike','Sam');
Now, set both the above arrays in array_merge() to concatenate them.
The syntax is as follows −
array_merge($yourFirstArrayName, $yourSecondArrayName);
Example
The PHP code is as follows
<!DOCTYPE html> <html> <body> <?php $nameArray1 = array('John','David'); $nameArray2 = array('Mike','Sam'); $result = array_merge($nameArray1, $nameArray2); print_r($result); ?> </body> </html>
Output
This will produce the following output
Array ( [0] => John [1] => David [2] => Mike [3] => Sam )
Advertisements