PHP array_merge() Function

PHP array_merge() Function

The array_merger() Function in PHP merges the elements of one or more arrays wherein the values of one are appended to the end of the previous one. It returns the resulting array after merging.

Syntax

array_merge (array[$array1], array[$array1], array[ array $... ] )

Parameter

array1(required)- This parameter represents the input array.

array2(optional)- This parameter signifies the second input array.

…(optional)- It represents one or more list of arrays to merge.

Return

This function returns the resulting array by merging one or more arrays.

Example 1

 

Output

Array 1: Array
 (
     [0] => Mango
     [1] => Dates
     [2] => Blueberry
     [3] => Orange
 ) 
 Array 2: Array
 (
     [0] => Potato
     [1] => Okra
     [2] => Brinjal
     [3] => Cabbage 
 )
 Merged Array: Array
 (
     [0] => Mango
     [1] => Dates 
     [2] => Blueberry
     [3] => Orange
     [4] => Potato
     [5] => Okra
     [6] => Brinjal
     [7] => Cabbage 
 ) 

Example 2

 array("Name" => "Reema"), 5);
 // initializing array2
 $array2 = array(10, "Student" => array("Name" => "Rakesh", "Mukesh"));
 // merging array1 and array2
 $result_array = array_merge($array1, $array2);
 print_r($result_array);
 ?> 

Output

Array
 (
     [Student] => Array
         (
             [Name] => Rakesh
             [0] => Mukesh
         )
     [0] => 5 
     [1] => 10
 ) 

Example 3

 "PHP");
 $result = array_merge($array1, $array2);
 print_r($result);
 ?> 

Output

Array
 (
     [0] => PHP
 ) 

Example 4

"Earth", 2=>"Saturn", 3=>"Venus", 4=>"Pluto");
 print_r($array1);
 echo("\nArray 2: ");
 //initializing the input array2 
 $array2 = array(1=>"Antarctica", 2=>"South America", 3=>"Europe", 4=>"Africa");
 print_r($array2);
 echo("\nArray 3: ");
 //initializing the input array3
 $array3 = array(1=>"India", 2=>"USA", 3=>"Nepal", 4=>"Russia"); 
 print_r($array3);
 //merging array1, array2 and array3
 $result_aaray = array_merge($array1, $array2,$array3);
 echo("\nMerged Array: ");
 print_r($result_aaray);
 ?> 

Output

Array 1: Array
 (
     [1] => Earth
     [2] => Saturn
     [3] => Venus
     [4] => Pluto
 ) 
 Array 2: Array
 (
     [1] => Antarctica
     [2] => South America
     [3] => Europe
     [4] => Africa 
 )
 Array 3: Array
 (
     [1] => India
     [2] => USA
     [3] => Nepal
     [4] => Russia 
 )
 Merged Array: Array
 (
     [0] => Earth
     [1] => Saturn
     [2] => Venus
     [3] => Pluto 
     [4] => Antarctica
     [5] => South America
     [6] => Europe
     [7] => Africa
     [8] => India
     [9] => USA
     [10] => Nepal 
     [11] => Russia
 ) 

Related Topics

PHP Magic Constants

There are various predefine PHP Magic Constants. It starts with underscore(__) and ends with double underscore.  Magic Constant Description __LINE__ To check current line number of the file. __FILE__ To check full path and filename of...

1 minute read.

PHP unset() Function

PHP unset() Function The unset() function in PHP is used to unset a given variable. This function destroys the specified variables. If this function is called inside any user-defined function, then it unsets the value associated with...

1 minute read.

PHP str_rot13() Function

PHP str_rot13() Function The str_rot13() function in PHP performs the ROT13 encoding on a string and returns the resulting string. The ROT13 encoding is used to shift every letter by 13 places in the alphabet...

1 minute read.

PHP convert_cyr_string() Function

PHP convert_cyr_string() Function The convert_cyr_string() function in PHP converts from one Cyrillic character-set to another. The supported characters set are given below: k - koi8-rw - windows-1251i - iso8859-5a - x-cp866d - x-cp866m...

1 minute read.

PHP strcoll() Function

PHP strcoll() Function The strcoll() function in PHP compares two strings. This function is case-sensitive but not binary-safe. It uses the current locale for doing the comparisons. Syntax strcoll ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter...

1 minute read.

PHP nl_langinfo() Function

PHP nl_langinfo() Function The nl_langinfo() function in PHP returns specific local information. This function does not work on Windows platforms. Syntax nl_langinfo ( int $item ) Parameter Item(required)- This parameter specifies the  integer value of the element or the constant name of the...

1 minute read.

PHP Switch Statement with Example

PHP switch statement The PHP switch statement is used to execute one condition from many conditions.It similar to if statements on the same expression. Syntax: switch(n) {             case1: statement if n=case1; break;             case2:             statement if n=case2; break;             case3:            ...

3 minutes read.

PHP substr_replace() Function

PHP substr_replace() Function The substr_replace() function in PHP replaces a part of a string with another string. Syntax substr_replace (mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) Parameter string(required)- This parameter represents the input string. replacement(required)- This parameter signifies the string to replace with. start(required)-...

2 minutes read.

PHP array_uintersect_assoc() Function

PHP array_uintersect_assoc() Function The array_uintersect_uassoc() function in PHP calculates the intersection of arrays by comparing the data and indexes by separate callback functions. It returns an array containing all the values of the first input...

2 minutes read.

PHP sprintf() Function

PHP sprintf() Function The sprintf() function in PHP is used to write a formatted string to a variable and returns a formatted string. PHP 4 and above versions support the sprintf() function. The related functions are...

8 minutes read.

PHP strrchr() Function

PHP strrchr() Function The strrchr() function in PHP finds the last occurrence of a character in the specified string and returns the portion of haystack, which starts at the last occurrence of the needle and goes until the end of haystack....

1 minute read.

PHP parse_str() Function

PHP parse_str() Function The parse_str() function in PHP parses a query string into variables. Syntax parse_str ( string $encoded_string [, array &$result ] ) Parameter encoded_string(required)- This parameter represents the input string. result(optional)- This parameter specifies the name of an...

1 minute read.

PHP array_search() Function

PHP array_search() Function The array_search() function in PHP searches the array for the specified value. This function returns the first corresponding key if the value is found. Syntax array_search ( mixed $needle , array $haystack [, bool $strict ])  Parameter needle(required)- This parameter represents the value to...

1 minute read.

PHP array_intersect() function

The array_intersect () function in PHP is used to compute the intersection of arrays. It returns an array containing all the entries from array1 that are present in all the other arrays. Syntax array_intersect ( array $array1 , array $array2 [, array $... ]) Parameter array1(required)- This parameter...

1 minute read.

Python Set pop() method

Python Set pop() method The set.pop() method in Python removes an arbitrary element (random item) from the set and returns the element removed. Syntax set.pop()  Parameter NA Return This method returns an arbitrary (random) element from the set. Example 1 #...

1 minute read.

PHP Forms

PHP form is used to take input from users, by using superglobals$_REQUEST, $_GET and $_POSTmethod. There are various request methods in PHP Get method Post method Put method Patch method Delete...

5 minutes read.

PHP array_multisort() Function

PHP array_multisort () Function The array_multisort() function in PHP sorts multiple or multi-dimensional arrays at once. Syntax array_multisort ( array &$array1 [, mixed $array1_sort_order [, mixed $array1_sort_flags [, mixed $... ]]] ) Parameter array1(required)- This parameter specified the input array to sort. array1_sort_order(optional)- This parameter specifies the order (ascending or descending)...

1 minute read.

PHP Arrays

PHP Array is usedto hold more than one value at a time.  In another words, we can say that an array stores multiple values in one single variable. Following are the...

2 minutes read.

PHP Indexed Array

In PHP, arrays are linear data structures that allow to store multiple elements of the same type under a single variable which helps in saving the time and effort of...

3 minutes read.

PHP array_pop() Function

PHP array_pop() Function The array_pop() function in PHP pops or removes the last element from the array shortening the array by one element. Syntax array_pop ( array &$array ) Parameter array(required)- This parameter represents the input array to get the value...

1 minute read.