• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / php array_merge() – Merge One or More Arrays Together by Appending

php array_merge() – Merge One or More Arrays Together by Appending

April 5, 2022 Leave a Comment

The php array_merge() function allows us to append the items of one or more arrays to the items of a given array.

$array1 = array("bear","elephant","lion");
$array2 = array(1,2,3);
$array3 = array("orange","red","yellow");

print_r(array_merge($array1,$array2,$array3));

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => lion
    [3] => 1
    [4] => 2
    [5] => 3
    [6] => orange
    [7] => red
    [8] => yellow
)

When working with collections of data in php, the ability to add to, remove from, and manipulate those collections is valuable.

One such operation which is useful is the ability to be able to append items to an array.

We can append the items of multiple arrays together using the php array_merge() function.

The array_merge() functions “merges” one or more arrays together where the values of the arrays are appended to each other to form a new array.

Below is a simple example of how to use the array_merge() function in php.

$array1 = array("bear","elephant","lion");
$array2 = array(1,2,3);
$array3 = array("orange","red","yellow");

print_r(array_merge($array1,$array2,$array3));

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => lion
    [3] => 1
    [4] => 2
    [5] => 3
    [6] => orange
    [7] => red
    [8] => yellow
)

Merging Arrays with Duplicate Keys with array_merge() in php

When working with array_merge(), there are a few things you need to be aware of when it comes to how the keys are determined after merging.

If the arrays you are merging with array_merge() are numeric, then the keys will be reindex with the keys starting from zero in the resulting array.

This was shown above in the first example. Below shows this example again, where all of the arrays have numeric keys.

$array1 = array("bear","elephant","lion");
$array2 = array("orange","red","yellow");

print_r($array1);
print_r($array2);
print_r(array_merge($array1,$array2));

// Output: 
Array
(
    [0] => bear
    [1] => elephant
    [2] => lion
)
Array
(
    [0] => orange
    [1] => red
    [2] => yellow
)
Array
(
    [0] => bear
    [1] => elephant
    [2] => lion
    [3] => orange
    [4] => red
    [5] => yellow
)

Things are a little more complicated when there are string keys in the arrays you are merging. If you are merging arrays that have keys which are the same, then the last value will overwrite the previous values.

Below is an example showing how array_merge() works with arrays that have string keys.

$array1 = array("black" => "bear", "grey" => "elephant", "yellow" => "lion");
$array2 = array("orange" => "orange", "red" => "tomato", "yellow" => "lemon");

print_r($array1);
print_r($array2);
print_r(array_merge($array1,$array2));

// Output: 
Array
(
    [black] => bear
    [grey] => elephant
    [yellow] => lion
)
Array
(
    [orange] => orange
    [red] => tomato
    [yellow] => lemon
)
Array
(
    [black] => bear
    [grey] => elephant
    [yellow] => lemon
    [orange] => orange
    [red] => tomato
)

As you can see, since both arrays have the key ‘yellow’, only the last value, “lemon” in the second array was kept after merging.

Hopefully this article has been useful for you to learn how to use the php array_merge() function.

Other Articles You'll Also Like:

  • 1.  php e – Using M_E and exp() Function to Get Euler’s Constant e
  • 2.  php acosh – Find Hyperbolic Arccosine of Number Using acosh() Function
  • 3.  php Move File from One Directory to Another Folder
  • 4.  php ceil – Round Up and Find the Ceiling of a Number in php
  • 5.  php asin – Find Arcsine and Inverse Sine of Number Using asin() Function
  • 6.  Get Last Element of Array in php
  • 7.  php max – Find Maximum Value of Array in php
  • 8.  Using isset() Function in php to Check if Variable is Defined
  • 9.  Remove Duplicates from Array in php with array_unique()
  • 10.  php Delete Files with php Unlink Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy