IT Learn

Top Menu

  • Home
  • Guest Posting
  • Contact Us

Main Menu

  • PHP
  • MySql
  • IT Learn
  • Home
  • Guest Posting
  • Contact Us

logo

Header Banner

IT Learn

  • PHP
  • MySql
  • IT Learn
PHP
Home›PHP›How to merge two array into single array in php?

How to merge two array into single array in php?

By IT Learn
April 11, 2023
418
0
Share:

Merging two arrays into a single array is a common task in PHP programming. It involves combining the elements of two arrays into a new array, which contains all the elements from both arrays. This process is particularly useful when working with large sets of data, such as user input, database results, or API responses. In this article, we will explore several methods for merging two arrays in PHP.

Method 1: Using the array_merge() Function

The array_merge() function is a built-in PHP function that can be used to merge two or more arrays into a single array. The function takes two or more arrays as arguments and returns a new array that contains all the elements from the input arrays.

The syntax for using array_merge() function is as follows:

In this example, $array1 and $array2 are the two arrays that we want to merge, and $result is the new array that will contain all the elements from both arrays.

Here is an example of how to use the array_merge() function:

$array1 = array('apple', 'banana', 'orange');
$array2 = array('kiwi', 'mango', 'pineapple');
$result = array_merge($array1, $array2);
print_r($result);

This will output the following:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => kiwi
    [4] => mango
    [5] => pineapple
)

In this example, the array_merge() function has merged the elements of $array1 and $array2 into a single array, $result.

Method 2: Using the + Operator

Another way to merge two arrays in PHP is by using the + operator. This method involves adding the elements of one array to another array using the + operator, which results in a new array that contains all the elements from both arrays.

Here is an example of how to use the + operator to merge two arrays:

$array1 = array('apple', 'banana', 'orange');
$array2 = array('kiwi', 'mango', 'pineapple');
$result = $array1 + $array2;
print_r($result);

This will output the following:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => kiwi
    [4] => mango
    [5] => pineapple
)

In this example, the + operator has merged the elements of $array1 and $array2 into a single array, $result.

Method 3: Using the array_push() Function

The array_push() function is another way to merge two arrays in PHP. This method involves pushing the elements of one array onto another array using the array_push() function, which results in a new array that contains all the elements from both arrays.

Here is an example of how to use the array_push() function to merge two arrays:

$array1 = array('apple', 'banana', 'orange');
$array2 = array('kiwi', 'mango', 'pineapple');
foreach ($array2 as $element) {
    array_push($array1, $element);
}
print_r($array1);

This will output the following:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => kiwi
    [4] => mango
    [5] => pineapple
)
Previous Article

How to Manage Session in PHP 8 ...

Next Article

328198701739408866

0
Shares
  • 0
  • +
  • 0
  • 0
  • 0
  • 0

IT Learn

General News Blog is fantastic platform for all our readers as we provide them with an ample of valuable information over a plethora of worldwide topics. It is a real honour for us to publish important news and current updates over matters like Business, Travel, Career Advice, Trading, Investment, Visa Rules, Health Tips and much more...

Related articles More from author

  • PHP

    Convert PHP Arrays to String with Implode() in PHP 8

    April 9, 2023
    By IT Learn
  • PHP

    How to Manage Session in PHP 8 Application

    April 9, 2023
    By IT Learn

Leave a reply Cancel reply

You might be interested

    Timeline

    • March 20, 2025

      576615001742432602

    • February 13, 2025

      328198701739408866

    • April 11, 2023

      How to merge two array into single array in php?

    • April 9, 2023

      How to Manage Session in PHP 8 Application

    • April 9, 2023

      Convert PHP Arrays to String with Implode() in PHP 8

    ABOUT US

    General News Blog is fantastic platform for all our readers as we provide them with an ample of valuable information over a plethora of worldwide topics. It is a real honour for us to publish important news and current updates over matters like Business, Travel, Career Advice, Trading, Investment, Visa Rules, Health Tips and much more…

    Timeline

    • March 20, 2025

      576615001742432602

    • February 13, 2025

      328198701739408866

    © Copyright IT Learn. All rights reserved.