Use PHP functions in JavaScript

JavaScript array_merge_recursive

Recursively merges elements from passed arrays into one array

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
function array_merge_recursive (arr1, arr2) {
    // Recursively merges elements from passed arrays into one array  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/array_merge_recursive    // +   original by: Subhasis Deb
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // -    depends on: array_merge
    // *     example 1: arr1 = {'color': {'favourite': 'read'}, 0: 5}    // *     example 1: arr2 = {0: 10, 'color': {'favorite': 'green', 0: 'blue'}}
    // *     example 1: array_merge_recursive(arr1, arr2)
    // *     returns 1: {'color': {'favorite': {0: 'red', 1: 'green'}, 0: 'blue'}, 1: 5, 1: 10}
    var idx = '';
     if (arr1 && Object.prototype.toString.call(arr1) === '[object Array]' && 
        arr2 && Object.prototype.toString.call(arr2) === '[object Array]') {
        for (idx in arr2) {
            arr1.push(arr2[idx]);
        }    } else if ((arr1 && (arr1 instanceof Object)) && (arr2 && (arr2 instanceof Object))) {
        for (idx in arr2) {
            if (idx in arr1) {
                if (typeof arr1[idx] == 'object' && typeof arr2 == 'object') {
                    arr1[idx] = this.array_merge(arr1[idx], arr2[idx]);                } else {
                    arr1[idx] = arr2[idx];
                }
            } else {
                arr1[idx] = arr2[idx];            }
        }
    }
 
    return arr1;}
external links: original PHP docs | raw js source

Examples

Running

1
2
3
arr1 = {'color': {'favourite': 'read'}, 0: 5}
arr2 = {0: 10, 'color': {'favorite': 'green', 0: 'blue'}}
array_merge_recursive(arr1, arr2)

Should return

1
{'color': {'favorite': {0: 'red', 1: 'green'}, 0: 'blue'}, 1: 5, 1: 10}

Dependencies

In order to use this function, you also need:

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have array_merge_recursive goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
سياحة وسفر
Apr 10th Permalink

q  If I might —perhaps you should consider adding a few images. I don’t mean to disrespect what you’ve said ; its very enlightening, indeed. However, I think would respond to it more positively if they could be something tangible to your ideas

Gravatar
Shannon
12 Feb '10 Permalink

q  Sorry I included the script tags without thinking.

[CODE]
var arr1 = {'color': {'favorite': 'red'}, 0: 5}, arr2 = {0: 10, 'color': {'favorite': 'green', 0: 'blue'}};
result = array_merge_recursive(arr1, arr2);
// result = {0:10,'color':{0:'blue','favorite':'green'}}

Gravatar
Shannon
12 Feb '10 Permalink

q  Am I doing some thing wrong? Using your example I get the following:
[CODE]<script>
var arr1 = {'color': {'favorite': 'red'}, 0: 5},
arr2 = {0: 10, 'color': {'favorite': 'green', 0: 'blue'}};
result = array_merge_recursive(arr1, arr2);
// result = {0:10,color:{0:blue,favorite:green}}
</script>[CODE]

I downloaded the full package so I should have all the dependencies.


Contribute a New function