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 Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | <html> <head> <title>Code4Example</title> </head> <body> <?php $array1 = array(1, "fruit" => "banana", 2, "monkey", 3); $array2 = array("a", "b", "fruit" => "apple", "city" => "paris", 4, "c"); echo "<pre>"; // Merging arrays together $result = array_merge($array1, $array2); print_r($result); ?> </body> </html> |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | Array ( [0] => 1 [fruit] => apple [1] => 2 [2] => monkey [3] => 3 [4] => a [5] => b [city] => paris [6] => 4 [7] => c ) |