Home » PHP

PHP array_merge() function with example

PHP array_merge() function: Here, we are going to learn about the array_merge() function with example in PHP.
Submitted by IncludeHelp, on February 21, 2019

PHP array_merge() function

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

Syntax:

    array_merge(array1, array2,...);

It accepts two or more arrays and returns a new array with merged elements.

Examples:

    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 code 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

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

PHP code 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

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

PHP Code 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

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

PHP Code 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

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

PHP Code 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

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






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.