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

PHP array_uintersect() Function The array_uintersect() function in PHP calculates the intersection of arrays by comparing the data by a callback function. It returns an array containing all the values of the first input array that are...

2 minutes read.

PHP array_udiff_assoc() Function

PHP array_udiff_assoc() Function The array_udiff_assoc () function in PHP computes the difference between the specified arrays with additional index check and compares the data by a callback function. It returns all the values from array1 that are not...

1 minute read.

PHP array_keys() Function

PHP array_keys() Function The array_keys() function  in PHP is used to return all the keys, numeric and string from the given array. Syntax array_keys ( array $array ) or array_keys ( array $array , mixed $search_value [, bool $strict = FALSE ] )  Parameter array(required)- This parameter specifies the input array search_value(optional)- This parameter specifies a value, where...

1 minute read.

PHP krsort() Function

PHP krsort() Function The krsort() function in PHP sorts an array by key in reverse order while maintaining the keys. Syntax krsort (array & $array[,int $sort_flags= SORT_REGULAR] ) Parameter array(required)- This parameter represents the input array. sort_flags(optional)- This...

3 minutes 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 array_diff_uassoc() Function

PHP array_diff_uassoc() Function The array_diff_uassoc () Function in PHP is used to compare the keys and values of two (or more) arrays and returns the differences (an array containing the entries from the first array that...

1 minute read.

PHP strspn() Function

PHP strspn() Function The strspn() function of PHP finds the length of the initial segment of string that contains only characters from the mask parameter. Syntax strspn(string$subject,string$mask[,int$start[,int$length]] ) Parameter subject(required)- This parameter represents the input string. mask(required)- This parameter specifies the characters to find. start (optional)- It signifies the position in the string to start...

1 minute read.

PHP str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

2 minutes read.

PHP settype() Function

PHP settype() Function The settype() function in PHP is used to set the type of variable var to type. Syntax settype ( mixed &$var , string $type ) Parameter var(required)- This parameter represents the variable being converted. type(required)- This parameter represents the type of variable that is needed.  The possible values of type are...

1 minute read.

PHP bin2hex() Function | Convert Binary to Hexadecimal

PHP bin2hex() Function The bin2hex() function in PHP converts the specified string value of ASCII characters to hexadecimal value. Syntax bin2hex(str) Parameter str(Required): This parameter represents the string to be converted into hexadecimal. Return  This function returns...

1 minute read.

PHP MySQL Database Tutorial

MySQL is the most popular database system that is used within PHP. Let's first understand about MySQL. What is MySQL? MySQL is open-source Relational Database Management System (RDMS). The name of MySQL is...

13 minutes read.

PHP stripcslashes() Function

PHP stripcslashes() Function The stripcslashes() function in PHP returns a string with backslashes(\n, \r ..., octal and hexadecimal representation) stripped off. Syntax stripcslashes ( string $str )  Parameter str(required)- This parameter signifies the string to be unescaped. Return This function returns the unescaped string with backslashes stripped off. Example 1   ...

1 minute read.

PHP quoted_printable_decode() Function

PHP quoted_printable_decode() Function The quote_printable_decode() function of PHP converts a quoted-printable string to an 8 bit string. It works similar to imap_qprint(), except this one does not require the IMAP module to work. Syntax quoted_printable_decode ( string $str ) Parameter str(required)- This parameter represents the...

1 minute read.

PHP boolval() Function

PHP boolval() Function The boolval() function in PHP is used to give the Boolean value for a given variable. Syntax boolval ( mixed $var ) Parameter var(required)- This parameter represents the value that is converted to a boolean. It can be a...

1 minute read.

WordPress Theme

Writing our WordPress Theme is relatively simple. Requirements: Before we begin, we need to know about the following set of requirements. We need to have full knowledge of WordPress setup, which is either...

2 minutes read.

PHP asort() function

PHP asort() function The asort() function in PHP is used to sort an array while maintaining the index association. Syntax asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  array(required)- This parameter represents the input array to sort. sort_flags (optional)- This parameter is used to...

1 minute read.

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 count() function

PHP count() function The count() function in PHP is used to count all elements in the specified array or something in an object. Syntax count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )   Parameter array_or_countable(required)– This parameter represents the input array or countable object. mode(optional)- This...

2 minutes read.

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

1 minute read.

PHP str_word_count() Function

PHP str_word_count() Function The str_word_count() function in PHP counts the number of words inside the string and returns the information about words used in a string. String str_word_count( string $string [, int $format [, string $charlist ]] ) Parameter string(required)- This parameter represents the input string. format(required)- This parameterspecifies the...

2 minutes read.