• 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
  • Ruby
  • 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 Absolute Value with abs Function
  • 2.  php is_string() Function – Check if Variable is String in php
  • 3.  Get Array Length with php count() Function
  • 4.  Get Query String from URL in php
  • 5.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 6.  Using php to Copy Files Easily from One Directory to Another
  • 7.  Remove HTML Tags from String in php
  • 8.  Remove First Character from String Using php
  • 9.  php pi – Get Value of pi Using php pi() Function
  • 10.  php in_array – Check If Value is in php Array

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy

x