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 quoted_printable_encode() Function

PHP quoted_printable_encode() Function The quoted_printable_encode() function in PHP converts a 8 bit string to a quoted-printable string. This function is similar to imap_8bit(), except this one does not require the IMAP module to work. Syntax quoted_printable_encode ( string $str ) Parameter str(required)- This parameter represents...

1 minute read.

PHP array_key_exists() function

The array_key_exists () function in PHP is used to check whether the specified key exists in the array or not. It returns a Boolean value TRUE if the given key exists else it returns FALSE. Syntax array_key_exists(mixed key,array$array) Parameter key(required)- This...

1 minute read.

PHP substr_count() Function

PHP substr_count() Function The substr_count() function in PHP counts the number of times a substring occurs in the specified string. Syntax substr_count ( string $haystack , string $needle [, int $offset  [, int $length ]] ) Parameter haystack(required)- This parameter signifies the string to search in. needle (required)- This parameter represents the substring to search...

1 minute read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute 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 str_repeat() Function

PHP str_repeat() Function The str_repeat() function in PHP repeats a string a specified number of times. Syntax str_repeat ( string $input , int $multiplier ) Parameter string(required)- This parameter signifies the string to be repeated. multiplier(required)- This parameter represents the number of times the string will be...

1 minute read.

PHP Comment

PHP commentis a line of code that is not executed. It is often used by developer to understand the code. PHP support single and multiple comments. In other language like C,...

1 minute read.

PHP shuffle() Function

PHP shuffle() Function The shuffle() function in PHP shuffles an array in a random array. Syntax shuffle ( array &$array )  Parameter array(required)- This parameter represents the input array.  Return This parameter returns a Boolean value TRUE on success or FALSE on failure. Example 1 Output Original Array: Array ...

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 doubleval() Function

PHP doubleval() Function The doubleval() function in PHP is used to give the float value of a variable. This function is an alias of floatval(). Syntax doubleval ( mixed $var ) Parameter var(required)- This parameter represents the name of the variable that...

1 minute read.

PHP For Loops

PHP for loop is used when the specified condition is predefined. Syntax: for (initialization; condition; increment/decrement ) { code to be executed; } Example <!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print counting using for loop"."<br>"; for($i=1;$i<=5;$i++){ echo $i."<br>"; } ?> </body> </html> Output Print counting using...

1 minute read.

PHP is_float() Function

The PHP is_float() function in PHP is used to  find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter var is...

1 minute read.

Multiple-Inheritance in PHP

Multiple-Inheritance in PHP Multiple-Inheritance is the resources of the Object Oriented Programming Languages in which child class or subclass can inherit the resources of the multiple parent classes or superclasses. PHP does...

3 minutes read.

PHP number_format() Function

PHP number_format() Function The number_format() function in PHP formats a number with grouped thousands. This function accepts one, two, or four parameters (not three): Syntax number_format ( float $number [, int $decimals ] )             or number_format ( float $number , int $decimals  , string $dec_point, string$thousands_sep ) Parameter number(required)- This parameter represents the number to be formatted. If...

1 minute read.

PHP array_unshift() Function

PHP array_unshift() Function The array_unshift () function in PHP prepends one or more elements to the beginning or front of an array. It returns the new number of elements in the array. Syntax array_unshift ( array &$array [, mixed $... ] ) Parameter array(required)- This array represents...

1 minute read.

PHP var_dump() Function

PHP var_dump() Function The var_dump() function in PHP is used to dump the information about a variable. It displays the structured information (arrays and objects) about one or more expressions. This function is also effective...

1 minute read.

PHP Constant

PHP constant is an identifier(name) for a simple value. The value does not change during the script. It starts with a letter or underscore not $ sign. The define()function is...

1 minute read.

Python if-else

Python if-else The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value. If the conditional expression...

2 minutes read.

PHP Associative Arrays

PHP associate array is used to access the elements with keys by “=>” symbol. There are two ways to create an associative array. 1st way: $age = array("John"=>"20", "Helena"=>"30", "Rasmus"=>"29"); 2nd way: $age['john'] = "20"; $age['Helena'] =...

1 minute read.

PHP str_replace() Function

PHP str_replace() Function The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive. Syntax str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter signifies the value to search. replace(required)-...

1 minute read.