Php

How To Merge Two Array or Multiple Array in PHP

This PHP tutorial helps to understand array_merge() php function. I will merge two or multiple arrays and store them into another array.

This method is used to merge or combine one or multiple arrays into one single array. This function adds elements of one array to the end of the previous array and returns a single resulting array.

This is very simple and easy in PHP language using array_merge() function. The key will be overridden, if the two elements have the same string keys then the latter value will be overridden.

How To Combine Two array in PHP

To combine single or many arrays, PHP has an in-built method called array merge(). We only need to pass the source arrays and the name of the destination array variable. This function joins the items or values of two or more arrays to form a single array. The array_merge() method added one array element to the end of the previous array.

Also checkout other related tutorials,

The Syntax of array_merge() Method

The array_merge() function takes a list of arrays separated by commas as a parameter. This method returns a new array with merged values of arrays passed in the parameter.

Syntax
array array_merge($array1, $array2, ......, $arrayn)

PHP Array Merge With Same Key

The array merge method will override two elements with the same string keys by the latter value, as I mentioned at the start of the tutorial.

Related Post

Source code –

<?php
$array1 = array(1, "fruit" => "banana", 2, "phpflow", 3);
$array2 = array("fruit" => "apple", "city" => "paris", 4, "c");
// Merging arrays together
$result = array_merge($array1, $array2);
print_r($result);
?>

The results –

Array
(
    [0] => 1
    [fruit] => apple
    [1] => 2
    [2] => phpflow
    [3] => 3
    [city] => paris
    [4] => 4
    [5] => c
)

Array Merge with integer keys

This function can also take arrays with integer keys as a parameter. The code>array merge()/code> method renumbers the key and appends it to the previous array. The key will begin at 0 and increment by 1 for subsequent entries.

Source code –

<?php
$array1 = array(1 => "apple", 2 => "banana", 4 => "Lychee");
// Merging arrays together
$result = array_merge($array1);
print_r($result);
?>

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => Lychee
)

Merge Two Array in PHP with integer keys

This example code helps to merge two multiple arrays using array_merge method.

Source code:

<?php
$array1 = array(0 => "apple", 2 => "banana", 3 => "Lychee", 5 => "avocado");
$array2 = array(0 => "red", 2 => "green", 4 => "yellow", 5 => "black");
// Merging arrays together
$result = array_merge($array1, $array2);
print_r($result);

Output:

Array
(
    [0] => apple
    [1] => banana
    [2] => Lychee
    [3] => avocado
    [4] => red
    [5] => green
    [6] => yellow
    [7] => black
)
Tags: php array

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

4 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

10 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

10 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

10 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

11 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

11 months ago

Categories