Use PHP functions in JavaScript

JavaScript array_keys

Return just the keys from the input array, optionally only for the specified search_value

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
function array_keys (input, search_value, argStrict) {
    // Return just the keys from the input array, optionally only for the specified search_value  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/array_keys    // +   original 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)
    // +   improved by: jd
    // +   improved by: Brett Zamir (http://brett-zamir.me)    // +   input by: P
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} );
    // *     returns 1: {0: 'firstname', 1: 'surname'}
    var search = typeof search_value !== 'undefined',        tmp_arr = [],
        strict = !!argStrict,
        include = true,
        key = '';
     if (input && typeof input === 'object' && input.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
        return input.keys(search_value, argStrict);
    }
 
    for (key in input) {        if (input.hasOwnProperty(key)) {
            include = true;
            if (search) {
                if (strict && input[key] !== search_value) {
                    include = false;                }
                else if (input[key] != search_value) {
                    include = false;
                }
            } 
            if (include) {
                tmp_arr[tmp_arr.length] = key;
            }
        }    }
 
    return tmp_arr;
}
external links: original PHP docs | raw js source

Examples

Running

1
array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} );

Should return

1
{0: 'firstname', 1: 'surname'}

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_keys 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
بوابة نعم
Apr 4th Permalink

q  I have a lot to benefit from this article and thank you for this wonderful effort to this article and will continue my many articles you have other

Gravatar
Brett Zamir
13 Jun '11 Permalink

q  @P: Fixed in Git. Thanks for the report!

Gravatar
P
12 Jun '11 Permalink

q  The raw seems to be different than the site's copy, and in the raw copy there's a cs variable that's unaccounted for.

Gravatar
Brett Zamir
18 Feb '11 Permalink

q  @jd: Yes, in this function, I see no reason we shouldn't return a genuine array, since PHP is not preserving keys of an original array here but is just starting fresh (and is only producing a sequential numeric array). I've fixed in Git. Some of our other functions, however, our constrained to return an object in order to preserve keys and/or to avoid returning confusing arrays which have "undefined" interspersed throughout.

Gravatar
jd
16 Feb '11 Permalink

q  Shouldn't line 11 be:

var tmp_arr = []
(i.e. not curly brackets)

Gravatar
Brett Zamir
12 May '10 Permalink

q  @JeromeM: Can you provide some sample data where it is not working? Are you using the latest version? (see "raw js source" link on this page) What errors? The example is working for me

Gravatar
JeromeM
12 May '10 Permalink

q  Doesn't seem to work on Safari 4 ..
Neither on Safari 3.x (i'm actually developping for iPhone / iPad).

Gravatar
Brett Zamir
12 Jan '10 Permalink

q  Hello Mickael,

This should definitely be working in Firefox, as that is what i use to test it. Can you please give example code which causes the problem for you? Are you using any other libraries with your code like Prototype?

Gravatar
Mickael
12 Jan '10 Permalink

q  Hi,
I test this function with Mozilla Firefox 3.5.7 but it does not work.
I think that Mozilla has a bug concerning "for in" syntax.

Thanks for all scripts, PHP.JS is a very good work :)

Gravatar
Kevin van Zonneveld
22 Jan '08 Permalink

q  @ Ates Goral: Nice work, added!

Gravatar
Ates Goral
22 Jan '08 Permalink

q  Hmmm. I should change that to:

[CODE="Javascript"]
const CASE_LOWER = 0;
const CASE_UPPER = 1;
[/CODE]

Gravatar
Ates Goral
22 Jan '08 Permalink

q  Hey Kevin, here's my implementation for array_change_key_case().

[CODE="Javascript"]
var CASE_LOWER = 0;
var CASE_UPPER = 1;

function array_change_key_case(array) {
// * example 1: array_change_key_case(42);
// * returns 1: false
// * example 2: array_change_key_case([ 3, 5 ]);
// * returns 2: {0: 3, 1: 5}
// * example 3: array_change_key_case({ FuBaR: 42 });
// * returns 3: {"fubar": 42}
// * example 4: array_change_key_case({ FuBaR: 42 }, CASE_LOWER);
// * returns 4: {"fubar": 42}
// * example 5: array_change_key_case({ FuBaR: 42 }, CASE_UPPER);
// * returns 5: {"FUBAR": 42}
// * example 6: array_change_key_case({ FuBaR: 42 }, 2);
// * returns 6: {"FUBAR": 42}

if (array instanceof Array) {
return array;
}

if (array instanceof Object) {
var case_fn = (arguments.length == 1 || arguments[1] == CASE_LOWER) ?
"toLowerCase" : "toUpperCase";
var ret = new Object();

for (var key in array) {
ret[key[case_fn]()] = array[key];
}

return ret;
}

return false;
}
[/CODE]


Contribute a New function