PHP Array array_merge() Function (With Examples)

In this tutorial, we will learn about the PHP array_merge() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP array_merge() function

The array_merge() function is used to merge two or more arrays, it returns a new array with merged elements.

Syntax

The syntax of the array_merge() function:

array_merge(array1, array2,...);

Parameters

The parameters of the array_merge() function:

  • array1, array2, ...: It accepts two or more arrays and returns a new array with merged elements.

Return Value

The return type of this method is array, it returns the resulting array. If called without any arguments, returns an empty array. [Source]

Sample Input/Output

Input:
$arr1 = array(10, 20, 30, 40, 50);
$arr2 = array(60, 70, 80, 90, 10);

Output:
Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
    [6] => 70
    [7] => 80
    [8] => 90
    [9] => 10
)

PHP array_merge() Function Example 1

Merging two indexed arrays.

<?php
$arr1 = array(10, 20, 30, 40, 50);
$arr2 = array(60, 70, 80, 90, 10);

//merging arrays
$arr3 = array_merge($arr1, $arr2);

//printing 
print_r ($arr3);
?>

Output

The output of the above example is:

Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [4] => 50
    [5] => 60
    [6] => 70
    [7] => 80
    [8] => 90
    [9] => 10
)

PHP array_merge() Function Example 2

Merging two associative arrays having different keys and values.

<?php
$arr1 = array("a" => "Hello", "b" => "Hi");
$arr2 = array("c" => "Bye!", "d" => "Nothing");

//merging arrays
$arr3 = array_merge($arr1, $arr2);

//printing 
print_r ($arr3);
?>

Output

The output of the above example is:

Array
(
    [a] => Hello
    [b] => Hi
    [c] => Bye!
    [d] => Nothing
)

PHP array_merge() Function Example 3

Merging two associative arrays having different keys and duplicate value

<?php
$arr1 = array("a" => "Hello", "b" => "Hi");
$arr2 = array("c" => "Hello", "d" => "Hi");

//merging arrays
$arr3 = array_merge($arr1, $arr2);

//printing 
print_r ($arr3);
?>

Output

The output of the above example is:

Array
(
    [a] => Hello
    [b] => Hi
    [c] => Hello
    [d] => Hi
)

PHP array_merge() Function Example 4

Merging two associative arrays having duplicate keys and unique values.

<?php
$arr1 = array("a" => "Hello", "b" => "Hi");
$arr2 = array("a" => "Okay!", "d" => "Nothing");

//merging arrays
$arr3 = array_merge($arr1, $arr2);

//printing 
print_r ($arr3);
?>

Output

The output of the above example is:

Array
(
    [a] => Okay!
    [b] => Hi
    [d] => Nothing
)

PHP array_merge() Function Example 5

Merging indexed and associative arrays.

<?php
$arr1 = array(10, 20, 30, 40);
$arr2 = array("c" => "Hello", "d" => "Hi");

//merging arrays
$arr3 = array_merge($arr1, $arr2);

//printing 
print_r ($arr3);
?>

Output

The output of the above example is:

Array
(
    [0] => 10
    [1] => 20
    [2] => 30
    [3] => 40
    [c] => Hello
    [d] => Hi
)

To understand the above examples, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.