Use PHP functions in JavaScript

JavaScript array_pop

Pops an element off the end of the array

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
function array_pop (inputArr) {
    // Pops an element off the end of the array  
    // 
    // version: 912.2221
    // discuss at: http://phpjs.org/functions/array_pop    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +      input by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)    // +   input by: Theriault
    // %        note 1: While IE (and other browsers) support iterating an object's
    // %        note 1: own properties in order, if one attempts to add back properties
    // %        note 1: in IE, they may end up in their former position due to their position
    // %        note 1: being retained. So use of this function with "associative arrays"    // %        note 1: (objects) may lead to unexpected behavior in an IE environment if
    // %        note 1: you add back properties with the same keys that you removed
    // *     example 1: array_pop([0,1,2]);
    // *     returns 1: 2
    // *     example 2: data = {firstName: 'Kevin', surName: 'van Zonneveld'};    // *     example 2: lastElem = array_pop(data);
    // *     returns 2: 'van Zonneveld'
    // *     results 2: data == {firstName: 'Kevin'}
    var key = '', lastKey = '';
     if (inputArr.hasOwnProperty('length')) {
        // Indexed
        if (!inputArr.length){
            // Done popping, are we?
            return null;        }
        return inputArr.pop();
    } else {
        // Associative
        for (key in inputArr) {            if (inputArr.hasOwnProperty(key)) {
                lastKey = key;
            }
        }
        if (lastKey) {            var tmp = inputArr[lastKey];
            delete(inputArr[lastKey]);
            return tmp;
        } else {
            return null;        }
    }
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
array_pop([0,1,2]);

Should return

1
2

» Example 2

Running

1
2
data = {firstName: 'Kevin', surName: 'van Zonneveld'};
lastElem = array_pop(data);

Should result in

1
data == {firstName: 'Kevin'}

Dependencies

No dependencies, you can use this function standalone.

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

Gravatar
Kevin van Zonneveld
2 Feb '09 Permalink

q  @ Brett Zamir: I missed that, I'll look into it.

Gravatar
Brett Zamir
2 Feb '09 Permalink

q  The harder part of fixing these functions goes beyond making them work with arrays and objects; if you really want them to be faithful to PHP, they need to force numerical indexes to start over at 0 (e.g., if you have an object which has numerical indexes starting at 5 or skipping around, etc.) as soon as these functions are used. I think we can use an inner function I developed for array_splice() to help with that.

Gravatar
Kevin van Zonneveld
31 May '08 Permalink

q  @ delete key in array: I'm not sure array_deletekey is a valid PHP functions ;) But in PHP, why don't you use unset() ?

Gravatar
delete key in array (not available in php!)
27 May '08 Permalink

q   The code beneath deletes the instance with key "todelete" in an array, in any position in the array(!). Even PHP doesn't have an function to accomplish this.

Alex Pot
Zinrijk Webtechniek
http://www.zinrijk.nl/webapplicaties
Haarlem, the Netherlands

1
2
3
4
56
7
8
9
1011
12
13
14
function array_deletekey( mixed_var, todelete) {
        last=mixed_var.length;
        narr=new Array();
        correction=0;
         for (x=0;x<last;x++)
        {
                if(mixed_var[x]!=todelete)narr[x-correction]=mixed_var[x];
                else correction++;
        } 
        mixed_var=narr;
        return mixed_var;
}


Contribute a New function