phpjs
Version:
84 lines (68 loc) • 2.48 kB
Markdown
layout: page
title: "JavaScript array_walk_recursive function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/array_walk_recursive:350
- /functions/view/array_walk_recursive
- /functions/view/350
- /functions/array_walk_recursive:350
- /functions/350
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's array_walk_recursive
{% codeblock array/array_walk_recursive.js lang:js https://raw.github.com/kvz/phpjs/master/functions/array/array_walk_recursive.js raw on github %}
function array_walk_recursive (array, funcname, userdata) {
// From: http://phpjs.org/functions
// + original by: Johnny Mast (http://www.phpvrouwen.nl)
// * example 1: array_walk_recursive ({'a': 'b', 'c': {'d': 'e'}}, 'void', 'userdata');
// * returns 1: true
// * example 2: array_walk_recursive ('a', 'void', 'userdata');
// * returns 2: false
var key;
if (typeof array !== 'object') {
return false;
}
for (key in array) {
if (typeof array[key] === 'object') {
return this.array_walk_recursive(array[key], funcname, userdata);
}
if (typeof userdata !== 'undefined') {
eval(funcname + '( array [key] , key , userdata )');
} else {
eval(funcname + '( userdata ) ');
}
}
return true;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/array/array_walk_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_walk_recursive.js)
### Example 1
This code
{% codeblock lang:js example %}
array_walk_recursive ({'a': 'b', 'c': {'d': 'e'}}, 'void', 'userdata');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
true
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
array_walk_recursive ('a', 'void', 'userdata');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
false
{% endcodeblock %}
### Other PHP functions in the array extension
{% render_partial _includes/custom/array.html %}