Use PHP functions in JavaScript

JavaScript reset

Set array argument's internal pointer to the first element and return it

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 reset (arr) {
    // Set array argument's internal pointer to the first element and return it  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/reset    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Legaev Andrey
    // +    revised by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: Uses global: php_js to store the array pointer
    // *     example 1: reset({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});    // *     returns 1: 'Kevin' 
    // 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 arrpos = pointers.indexOf(arr);
    if (Object.prototype.toString.call(arr) !== '[object Array]') {
        for (var k in arr) {
            if (pointers.indexOf(arr) === -1) {
                pointers.push(arr, 0);            } else {
                pointers[arrpos + 1] = 0;
            }
            return arr[k];
        }        return false; // Empty
    }
    if (arr.length === 0) {
        return false;
    }    pointers[arrpos + 1] = 0;
    return arr[pointers[arrpos + 1]];
}
external links: original PHP docs | raw js source

Examples

Running

1
reset({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});

Should return

1
'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 reset 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
Brett Zamir
20 Dec '09 Permalink

q  @Rey: Did some of your post get cut off? No idea what you're talking about... We don't want to install anything to test too, so please include whatever code you want to highlight here (or use a site like pastebin.org and refer us to it)

Gravatar
Rey S.
19 Dec '09 Permalink

q  javascript: a76883600569_do_hosp("heal","7667eddaf89376258610b9df58d1d54dda64721c");

As to use this string and to make it to work in a platform of a game of Facebook. The code of 40 figures and the key of the personage.
Thanks
http://apps.facebook.com/guerre-di-banda/hospital.php


Contribute a New function