Use PHP functions in JavaScript

JavaScript array_slice

Returns elements specified by offset and length

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
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
function array_slice (arr, offst, lgth, preserve_keys) {
    // Returns elements specified by offset and length  
    // 
    // version: 911.815
    // discuss at: http://phpjs.org/functions/array_slice    // +   original by: Brett Zamir (http://brett-zamir.me)
    // -    depends on: is_int
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // %          note: Relies on is_int because !isNaN accepts floats     // *     example 1: array_slice(["a", "b", "c", "d", "e"], 2, -1);
    // *     returns 1: {0: 'c', 1: 'd'}
    // *     example 2: array_slice(["a", "b", "c", "d", "e"], 2, -1, true);
    // *     returns 2: {2: 'c', 3: 'd'}
    /*    if ('callee' in arr && 'length' in arr) {
        arr = Array.prototype.slice.call(arr);
    }
    */
       var key = '';
 
    if (!(arr instanceof Array) || (preserve_keys && offst !== 0)) { // Assoc. array as input or if required as output
        var lgt =0, newAssoc = {};
        for (key in arr) {            //if (key !== 'length') {
            lgt += 1;
            newAssoc[key] = arr[key];
            //}
        }        arr = newAssoc;
 
        offst = (offst < 0) ? lgt + offst : offst;
        lgth  = lgth === undefined ? lgt : (lgth < 0) ? lgt + lgth - offst : lgth;
         var assoc = {};
        var start = false, it=-1, arrlgth=0, no_pk_idx=0;
        for (key in arr) {
            ++it;
            if (arrlgth >= lgth) {                break;
            }
            if (it == offst){
                start = true;
            }            if (!start) {
                continue;
            }
            ++arrlgth;
            if (this.is_int(key) && !preserve_keys) {                assoc[no_pk_idx++] = arr[key];
            } else {
                assoc[key] = arr[key];
            }
        }        //assoc.length = arrlgth; // Make as array-like object (though length will not be dynamic)
        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);    }
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
array_slice(["a", "b", "c", "d", "e"], 2, -1);

Should return

1
{0: 'c', 1: 'd'}

» Example 2

Running

1
array_slice(["a", "b", "c", "d", "e"], 2, -1, true);

Should return

1
{2: 'c', 3: 'd'}

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_slice 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

No comments yet. Be the first!


Contribute a New function

Download

Download

There is a wide variety of packages if the default doesn't suit you.
You can also compile your own package to avoid any overhead.

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !

RSS

Tweets

Comments

Who uses php.js

If you use php.js, let us know and get linked.

Progress

php.js is complete for 83.7%

php.js on

Discuss php.js' future at Google Groups
Help improve php.js on github



Powered by php.js
Stats