Wednesday, 24 September 2014

array_merge_recursive in PHP

array_merge_recursive() function merges different arrays into single array.

PHP array_merge_recursive() function is similar to array_merge() function. 

Just contrast between these two is on case of two or more array items have same key,  array_merge() function overrides the keys and array_merge_recursive() function makes array of values.

Syntax:

array_merge_recursive(array1,array2,array3...)
Parameters Description:
array1 : Required. Specifies an array
array2 : Optional. Specifies an array
array3 : Optional. Specifies an array

Tip : In case of assigning a single array to array_merge_recursive() function, output will be the same return same output as  array_merge() function.

Example:

<?php
$daysarr1 = array( "a"=>"Sunday","b"=>"Monday" );
$daysarr2 = array( "c"=>"Tuesday","b"=>"Thursday");
print_r( array_merge_recursive($daysarr1,$daysarr2) );
?>

O/P:


Array ( 
          [a] => Sunday 
          [b] => Array (
                              [o] => Monday 
                              [1] => Thursday
                            )
          [c] => Tuesday 
       )

0 comments:

Post a Comment