Difference between array_merge and array_merge_recursive

Difference between array_merge and array_merge_recursive

Difference between array_merge and array_merge_recursive:
============================================
array_merge()
===========
 
 It merges the elements of one or more arrays. The elements and values are appended at the end of the previous array.
Syntax:
=====
              array array_merge ( array array1, array array2 [, array …])

            If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays have the same numeric key, the

later value will not overwrite the original value, but will be appended.
Example:
======

$arr1 = array(22,33,count => 4);
$arr2 = array(44,55,count=>5,color=”red”);
print_r(array_merge($arr1,$arr2);
The result will be:
 Array
(
    [0] => 22
    [1] => 33
    [count]=5
    [2] => 44
    [3] => 55
    [color] => red
)
 

array_merge_recursive:
=================
 It merges the two or more arrays recurssively.

Syntax:
=====
                 array array_merge_recursive ( array array1, array array2 [, array …])

           If the input arrays have the same string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values

is an array itself, the function will merge it with a corresponding entry in another array too. If, however, the arrays have the same numeric key, the later value will not

overwrite the original value, but will be appended.

Example:
======

$arr1 = array(22,33,count => 4);
$arr2 = array(44,55,count=>5,color=>”red”);
print_r(array_merge_recursive($arr1,$arr2));

The result will be:
Array
(
    [0] => 22
    [1] => 33
    [count] => Array
        (
            [0] => 4
            [1] => 5
        )

    [2] => 44
    [3] => 55
    [color] => red
)

========================================================  

Leave a Reply

You must be logged in to post a comment.


All material @ copyrighted by chrisranjana.com. If you want to link to this article you are welcome to do so. Unauthorized publication is strictly prohibited. This developer tutorial website contains articles by Php programmers , Software developers, Mysql programmers and asp c# programmers. This website also contains ajax tutorials and advanced mysql sql stored procedures and functions tutorials and sample codes.