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
key:
Edit: key
Function:
key
View key
Edit key
Edit code
Edit tests
Edit benchmark
View latest code
Edit latest code
Benchmark key
Comment on key
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:
Return the key of the element currently pointed to by the internal array pointer.
Edit function
Code
:
function key (arr) { // http://jsphp.co/jsphp/fn/view/key // + original by: Brett Zamir (http://brett-zamir.me) // + input by: Riddler (http://www.frontierwebdev.com/) // + bugfixed by: Brett Zamir (http://brett-zamir.me) // % note 1: Uses global: php_js to store the array pointer // * example 1: array = {fruit1: 'apple', 'fruit2': 'orange'} // * example 1: key(array); // * returns 1: 'fruit1' // BEGIN REDUNDANT this.php_js = this.php_js || {}; this.php_js.pointers = this.php_js.pointers || []; var indexOf = function (value) { for (var i = 0, length = this.length; i < length; i++) { if (this[i] === value) { return i; } } return -1; }; // END REDUNDANT var pointers = this.php_js.pointers; if (!pointers.indexOf) { pointers.indexOf = indexOf; } if (pointers.indexOf(arr) === -1) { pointers.push(arr, 0); } var cursor = pointers[pointers.indexOf(arr) + 1]; if (Object.prototype.toString.call(arr) !== '[object Array]') { var ct = 0; for (var k in arr) { if (ct === cursor) { return k; } ct++; } return false; // Empty } if (arr.length === 0) { return false; } return cursor; }
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.