PHP's array_slice in JavaScript

Here’s what our current JavaScript equivalent to PHP's array_slice looks like.

module.exports = function array_slice (arr, offst, lgth, preserveKeys) { // eslint-disable-line camelcase
// discuss at: https://locutus.io/php/array_slice/
// original by: Brett Zamir (https://brett-zamir.me)
// input by: Brett Zamir (https://brett-zamir.me)
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// note 1: Relies on is_int because !isNaN accepts floats
// example 1: array_slice(["a", "b", "c", "d", "e"], 2, -1)
// returns 1: [ 'c', 'd' ]
// example 2: array_slice(["a", "b", "c", "d", "e"], 2, -1, true)
// returns 2: {2: 'c', 3: 'd'}
const isInt = require('../var/is_int')
/*
if ('callee' in arr && 'length' in arr) {
arr = Array.prototype.slice.call(arr);
}
*/
let key = ''
if (Object.prototype.toString.call(arr) !== '[object Array]' || (preserveKeys && offst !== 0)) {
// Assoc. array as input or if required as output
let lgt = 0
const newAssoc = {}
for (key in arr) {
lgt += 1
newAssoc[key] = arr[key]
}
arr = newAssoc
offst = (offst < 0) ? lgt + offst : offst
lgth = lgth === undefined ? lgt : (lgth < 0) ? lgt + lgth - offst : lgth
const assoc = {}
let start = false
let it = -1
let arrlgth = 0
let noPkIdx = 0
for (key in arr) {
++it
if (arrlgth >= lgth) {
break
}
if (it === offst) {
start = true
}
if (!start) {
continue
}++arrlgth
if (isInt(key) && !preserveKeys) {
assoc[noPkIdx++] = arr[key]
} else {
assoc[key] = arr[key]
}
}
// Make as array-like object (though length will not be dynamic)
// assoc.length = arrlgth;
return assoc
}
if (lgth === undefined) {
return arr.slice(offst)
} else if (lgth >= 0) {
return arr.slice(offst, offst + lgth)
} else {
return arr.slice(offst, lgth)
}
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/array/array_slice'). You could also require the array module in full so that you could access array.array_slice instead.

If you intend to target the browser, you can then use a module bundler such as Parcel, webpack, Browserify, or rollup.js. This can be important because Locutus allows modern JavaScript in the source files, meaning it may not work in all browsers without a build/transpile step. Locutus does transpile all functions to ES5 before publishing to npm.

PHP arrays and JavaScript

Please note that Locutus uses JavaScript objects as substitutes for PHP arrays, they are the closest we can get to this hashtable-like data structure without rolling our own. While many JavaScript implementations preserve the order of object properties, the ECMAScript Language Specification explicitly states that:

The mechanics and order of enumerating the properties is not specified.

So don't use this for anything serious if you rely on the order to be consistent accross platforms.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

Notes

  • Relies on is_int because !isNaN accepts floats

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1array_slice(["a", "b", "c", "d", "e"], 2, -1)[ 'c', 'd' ]
2array_slice(["a", "b", "c", "d", "e"], 2, -1, true){2: 'c', 3: 'd'}

« More PHP array functions


Star