Q:

How to merge two or more arrays into one array in PHP

0

How to merge two or more arrays into one array in PHP

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Use the PHP array_merge() function

You can use the PHP array_merge() function to merge the elements or values of two or more arrays together into a single array. The merging is occurring in such a way that the values of one array are appended to the end of the previous array. Let's check out an example:

<?php
$array1 = array(1, "fruit" => "banana", 2, "monkey", 3);
$array2 = array("a", "b", "fruit" => "apple", "city" => "paris", 4, "c");
 
// Merging arrays together
$result = array_merge($array1, $array2);
print_r($result);
?>

Note: If the input arrays contain the same string keys, then the later value for that key will overwrite the previous one during the merging process.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

PHP  and MySQL Frequently Asked Questions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
How to sort an array values alphabetically in PHP... >>
<< How to add elements to the end of an array in PHP...