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

Return

This function returns a resulting array merging the elements of one or arrays together or returns an empty array of no arguments are passed.

Example 1

 

Output

Array 1: Array
 (
     [0] => Watermelon
     [1] => Dates
     [2] => Blueberry
     [3] => Orange
 ) 
 Array 2: Array
 (
     [0] => Peas
     [1] => Bottle guard
     [2] => Capsicum
     [3] => Cabbage 
 ) 
 Merged Array: Array
 (
     [0] => Watermelon
     [1] => Dates
     [2] => Blueberry 
     [3] => Orange
     [4] => Peas
     [5] => Bottle guard
     [6] => Capsicum
     [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_recursive($array1, $array2); 
 print_r($result_array);
 ?> 

Output

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

Example 3

 

Output

Array
 (
     [0] => 
     [1] => 
 ) 

Example 4

"Earth", 2=>"Neptune", 3=>"Saturn");
 print_r($array1);
 echo("\nArray 2: ");
 //initializing the input array2 
 $array2 = array(1=>"Asia", 2=>"Antarctica", 3=>"Europe", 4=>"Africa");
 print_r($array2);
 echo("\nArray 3: ");
 //initializing the input array3
 $array3 = array(1=>"India", 2=>"UK", 3=>"Canada", 4=>"Botswana");
 print_r($array3); 
 //merging array1, array2 and array3
 $result_aaray = array_merge_recursive($array1, $array2,$array3);
 echo("\nMerged Array: ");
 print_r($result_aaray);
 ?> 

Output

Array 1: Array
 (
     [1] => Earth
     [2] => Neptune
     [3] => Saturn
 )
 Array 2: Array 
 (
     [1] => Asia
     [2] => Antarctica
     [3] => Europe
     [4] => Africa
 ) 
 Array 3: Array
 (
     [1] => India
     [2] => UK 
     [3] => Canada
     [4] => Botswana
 )
 Merged Array: Array
 ( 
     [0] => Earth
     [1] => Neptune
     [2] => Saturn
     [3] => Asia
     [4] => Antarctica
     [5] => Europe
     [6] => Africa
     [7] => India 
     [8] => UK
     [9] => Canada
     [10] => Botswana 

Related Topics

PHP strip_tags() Function

PHP strip_tags() Function The strip_tags() function in PHP is sued to strip a string from HTML, XML, and PHP tags. It is a binary-safe function. Syntax strip_tags ( string $str [, string $allowable_tags ] ) Parameter str(required)- This parameter specifies the input string. allowable_tags(optional)- This parameter represents...

1 minute read.

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 if..Else..Elseif

PHP conditional statements are used to test the conditions. It supports different types of conditional statement. Following are the types of conditional statement if statement ..else statement ..elseif....else statement switch statement PHP if Statement PHP...

2 minutes read.

PHP foreach Loop

PHP foreach Loop with Example PHP foreach loop is used to loop through the associative arrays. Syntax: foreach($array as $key => $value) { //Statement } $array = associative array. $key = array key. &value =...

2 minutes read.

PHP print() Function

PHP print() Function The print() function in PHP is used to output one or more string. Syntax print ( string $arg ) Parameter arg(required)- This parameter represents the input data. It can take one or more arguments. Return This function returns an integer value 1. Example...

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

PHP vfprintf() Function The vfprintf() function in PHP writes a formatted string to a specified output stream. Syntax vfprintf ( resource $handle , string $format , array $args )  Parameter handle(required)- This parameter represents where to write the string. format(required)- This parameter specifies the string and how to format the...

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

PHP sha1() Function The sha1() in PHP calculates the SHA-1 hash of a string. Syntax sha1 ( string $str [, bool $raw_output = FALSE ] )  Parameter str (required)- This parameter represents the input string. raw_output(optional)- This parameter takes Boolean value and returns the digest in raw binary format...

1 minute read.

PHP array_diff_key () Function

PHP array_diff_key () Function The array_diff_key () Function in PHP compares the keys from array1 against the keys from arrays. It returns an array containing all the entries from array1 whose keys are absent from all other arrays. This function is like array_diff () except that...

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.

PHP strnatcmp() Function

PHP strnatcmp() Function The strnatcmp() function in PHP compares two strings using a "natural" algorithm. It is case-sensitive. Syntax strnatcmp ( string $str1 , string $str2 ) Parameter str1(required)- This parameter represents the first string. str2(required)- This parameter represents the second string. Return This function returns a value less than(...

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

PHP array_values() Function The array_values () function in PHP returns all the values from the array and indexes the array numerically. Syntax array_values ( array $array ) Parameter array(required)- This array represents the input array. Return This function returns an indexed array of values. Example 1 Output Input Array: Array ...

1 minute read.

PHP sha1_file() Function

PHP sha1_file() Function The sha1_file() in PHP calculates the sha1 hash of a file and returns the hash (40-character hexadecimal number). Syntax sha1_file ( string $filename [, bool $raw_output] )  Parameter filename(required)- This parameter represents the filename of the file to hash. raw_output(optional)- This...

1 minute read.

PHP similar_text() Function

PHP similar_text() Function The similar_text() function in PHP calculates the similarity between two strings. Syntax similar_text ( string $first , string $second [, float &$percent ] ) Parameter first(required): This parameter represents the first string to be compared. second(required): This parameter represents the second string to be compared. percent(optional): This...

1 minute read.

PHP array_column() Function

PHP array_column() Function The array_column() function in PHP is used to return the values from a single column specified in the input array. Syntax array_column (array $input, mixed $column_key [, mixed $index_key]) Parameter input(required)- This parameter represents a multi-dimensional array or an array of...

1 minute read.

PHP uasort() Function

PHP uasort() Function The uasort() function in PHP sorts an array with a user-defined comparison function and maintains the index association. Syntax uasort ( array &$array , callable $value_compare_func ) Parameter array(required)- This parameter represents the input array. value_compare_func(required)- This parameter represents a string that describe a...

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