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

PHP array() function The array() function in PHP is used to create an array. Syntax array ([ mixed $... ] ) Parameter …(optional)- This parameter represents the elements’ values along with keys of the array. The syntax of this is as follows: "index => values"...

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

PHP strrrev () Function The strrev() function in PHP reverses a string. Syntax strrev ( string $string ) Parameter string(required)- This parameter represents the input string to be reversed. Return This function returns the reversed string. Example 1 Output Initial string: Hello...

1 minute read.

PHP unserialize() Function

PHP unserialize() Function The unserialize() function in PHP takes a single serialized variable and converts it back into a PHP value. Syntax unserialize ( string $str [, array $options ] ) Parameter value(required)- This parameter represents the serialized string. options(optional)- This parameter signifies any options to be...

1 minute read.

PHP strcasecmp() Function

PHP strcasecmp() Function The strcasecmp() function in PHP compares two strings. It is a binary-safe case-insensitive string function. Syntax strcasecmp ( 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.

PHP echo() Function

PHP echo() Function The echo() function in PHP outputs or displays one or more strings. The major differences between print() function and echo() function is that echo accepts an argument list and doesn't have a return value. Also, it is slightly...

1 minute read.

PHP metaphone() Function

PHP metaphone() Function The metaphone() function in PHP calculates the metaphone key (representing how a string sounds or pronounced) of a string. This function is used to text search and text matching or spelling...

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

PHP array_replace() Function The array_replace() function in PHP replaces the elements from passed arrays into the first array with values having the same keys in each of the following arrays. Syntax array_replace ( array $array1 [, array $... ] ) Parameter array(required)- This parameter represents...

1 minute read.

PHP key() Function

PHP key() Function The key() function in PHP fetches a key from an array and returns the index element of the current array position. Syntax key ( array $array ) Parameter array(required)- This parameter represents the input array. Return This function returns the key of the...

1 minute read.

PHP Continue

What is continue in PHP? Inside the loop, the continue statement is used to control the loop in preprocessor hypertext. PHP utilizes the continue keyword to implement the continue statement, which...

3 minutes read.

PHP chunk_split() Function

PHP chunk_split() Function The chunk_split() function in PHP splits a string into a series of smaller parts. Syntax chunk_split(string,length,end) Parameter string(required)- It specifies the string to split. length(optional)- This parameter represents a number that defines the...

1 minute 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 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 break

What is Break in PHP? Inside the loop, the break statement is used to control the loop in preprocessor hypertext. PHP utilizes the break keyword to implement the break statement, which...

3 minutes read.

WordPress Plugin

WordPress Plugin is a process used to add or extend the functionality which can be enabled or disabled, and it does not interfere with the software and its code. WordPress is lightweight and smooth...

2 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 array_diff_assoc() Function

PHP array_diff_assoc() Function The array_diff_assoc() Function in PHP compares the difference of arrays and returns an array containing all the values from array1 that are not present in any of the other arrays. Syntax array_diff_assoc ( array $array1 , array $array2 [, array $... ] ) Parameter array1(required)- This parameter signifies...

1 minute read.

PHP str_ireplace() Function

PHP str_ireplace() Function The PHP str_ireplace() function in PHP replaces some characters with some other characters in a string. Syntax str_ireplace ( mixed $search , mixed  $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter specifies the value being searched for. replace(required)- This parameter represents the replacement value...

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