You are not logged in.
Login
or
Register
.
JsPHP: a JavaScript library providing the PHP API
Home
Register
Demo
Categories
Functions
Contributors
Download
Links
Comments
FAQ
Contact
View
Edit
Benchmark
Revisions
Developers
Dependencies
Talk
Links
array_shift:
Edit: array_shift
Function:
array_shift
View array_shift
Edit array_shift
Edit code
Edit tests
Edit benchmark
View latest code
Edit latest code
Benchmark array_shift
Comment on array_shift
Comment on the latest code
View revisions
View versions
View contributors
View dependencies
View comments
View links
Download production code
Download development code
View the PHP docs
View phpjs.org
View phpjs.org raw code
Description:
Pops an element off the beginning of the array.
Edit function
Code
:
function array_shift (inputArr) { // http://jsphp.co/jsphp/fn/view/array_shift // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Martijn Wieringa // % note 1: Currently does not handle objects // * example 1: array_shift(['Kevin', 'van', 'Zonneveld']); // * returns 1: 'Kevin' var props = false, shift = undefined, pr = '', allDigits = /^\d$/, int_ct = -1, _checkToUpIndices = function (arr, ct, key) { // Deal with situation, e.g., if encounter index 4 and try to set it to 0, but 0 exists later in loop (need to // increment all subsequent (skipping current key, since we need its value below) until find unused) if (arr[ct] !== undefined) { var tmp = ct; ct += 1; if (ct === key) { ct += 1; } ct = _checkToUpIndices(arr, ct, key); arr[ct] = arr[tmp]; delete arr[tmp]; } return ct; }; if (inputArr.length === 0) { return null; } if (inputArr.length > 0) { return inputArr.shift(); } /* UNFINISHED FOR HANDLING OBJECTS for (pr in inputArr) { if (inputArr.hasOwnProperty(pr)) { props = true; shift = inputArr[pr]; delete inputArr[pr]; break; } } for (pr in inputArr) { if (inputArr.hasOwnProperty(pr)) { if (pr.search(allDigits) !== -1) { int_ct += 1; if (parseInt(pr, 10) === int_ct) { // Key is already numbered ok, so don't need to change key for value continue; } _checkToUpIndices(inputArr, int_ct, pr); arr[int_ct] = arr[pr]; delete arr[pr]; } } } if (!props) { return null; } return shift; */ }
You have to be logged in to edit functions.
[
top
]
Comments
There are no comments yet, be the first!
Please
Login
or
Register
to post comments.