Skip to content

Instantly share code, notes, and snippets.

@JoshOldenburg
Created April 12, 2013 00:25
Show Gist options
  • Save JoshOldenburg/5368293 to your computer and use it in GitHub Desktop.
Save JoshOldenburg/5368293 to your computer and use it in GitHub Desktop.
phpjs array_merge, formatted w/ tabs
function array_merge() {
// https://github.com/kvz/phpjs/blob/master/functions/array/array_merge.js
var args = Array.prototype.slice.call(arguments);
var argl = args.length;
var arg;
var retObj = {};
var k = '';
var argil = 0;
var j = 0;
var i = 0;
var ct = 0;
var toStr = Object.prototype.toString;
var retArr = true;
for (i = 0; i < argl; i++) {
if (toStr.call(args[i]) !== '[object Array]') {
retArr = false;
break;
}
}
if (retArr) {
retArr = [];
for (i = 0; i < argl; i++) {
retArr = retArr.concat(args[i]);
}
return retArr;
}
for (i = 0, ct = 0; i < argl; i++) {
arg = args[i];
if (toStr.call(arg) === '[object Array]') {
for (j = 0, argil = arg.length; j < argil; j++) {
retObj[ct++] = arg[j];
}
}
else {
for (k in arg) {
if (arg.hasOwnProperty(k)) {
if (parseInt(k, 10) + '' === k) {
retObj[ct++] = arg[k];
}
else {
retObj[k] = arg[k];
}
}
}
}
}
return retObj;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment