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
function array_keys (input, search_value, argStrict) {
    // Return just the keys from the input array, optionally only for the specified search_value  
    // 
    // version: 909.322
    // 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)
    // *     example 1: array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} );
    // *     returns 1: {0: 'firstname', 1: 'surname'}    
    var tmp_arr = {}, strict = !!argStrict, include = true, cnt = 0;
    var key = '';
    
    for (key in input) {        include = true;
        if (search_value != undefined) {
            if (strict && input[key] !== search_value){
                include = false;
            } else if (input[key] != search_value){                include = false;
            }
        }
        
        if (include) {            tmp_arr[cnt] = key;
            cnt++;
        }
    }
        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
Brett Zamir
Jan 12th 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
Jan 12th 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:

1
2
const CASE_LOWER = 0;
const CASE_UPPER = 1;

Gravatar
Ates Goral
22 Jan '08 Permalink

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

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
35
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;
}


Contribute a New function

Download

Download

There is a wide variety of packages if the default doesn't suit you.
You can also compile your own package to avoid any overhead.

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !

RSS

Tweets

Comments

Who uses php.js

If you use php.js, let us know and get linked.

Progress

php.js is complete for 83.7%

php.js on

Discuss php.js' future at Google Groups
Help improve php.js on github



Powered by php.js
Stats