phpjs
Version:
76 lines (62 loc) • 2.76 kB
Markdown
layout: page
title: "JavaScript array_replace_recursive function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/array_replace_recursive:860
- /functions/view/array_replace_recursive
- /functions/view/860
- /functions/array_replace_recursive:860
- /functions/860
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's array_replace_recursive
{% codeblock array/array_replace_recursive.js lang:js https://raw.github.com/kvz/phpjs/master/functions/array/array_replace_recursive.js raw on github %}
function array_replace_recursive (arr) {
// + original by: Brett Zamir (http://brett-zamir.me)
// * example 1: array_replace_recursive({'citrus' : ["orange"], 'berries' : ["blackberry", "raspberry"]}, {'citrus' : ['pineapple'], 'berries' : ['blueberry']});
// * returns 1: {citrus : ['pineapple'], berries : ['blueberry', 'raspberry']}
var retObj = {},
i = 0,
p = '',
argl = arguments.length;
if (argl < 2) {
throw new Error('There should be at least 2 arguments passed to array_replace_recursive()');
}
// Although docs state that the arguments are passed in by reference, it seems they are not altered, but rather the copy that is returned (just guessing), so we make a copy here, instead of acting on arr itself
for (p in arr) {
retObj[p] = arr[p];
}
for (i = 1; i < argl; i++) {
for (p in arguments[i]) {
if (retObj[p] && typeof retObj[p] === 'object') {
retObj[p] = this.array_replace_recursive(retObj[p], arguments[i][p]);
} else {
retObj[p] = arguments[i][p];
}
}
}
return retObj;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/array/array_replace_recursive.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/array/array_replace_recursive.js)
### Example 1
This code
{% codeblock lang:js example %}
array_replace_recursive({'citrus' : ["orange"], 'berries' : ["blackberry", "raspberry"]}, {'citrus' : ['pineapple'], 'berries' : ['blueberry']});
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
{citrus : ['pineapple'], berries : ['blueberry', 'raspberry']}
{% endcodeblock %}
### Other PHP functions in the array extension
{% render_partial _includes/custom/array.html %}