Use PHP functions in JavaScript

JavaScript array_change_key_case

Retuns an array with all string keys lowercased [or uppercased]

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
function array_change_key_case (array) {
    // Retuns an array with all string keys lowercased [or uppercased]  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/array_change_key_case    // +   original by: Ates Goral (http://magnetiq.com)
    // +   improved by: marrtins
    // *     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}
    var case_fn, tmp_ar = {}, argc = arguments.length, argv = arguments, key; 
    if (array instanceof Array) {
        return array; 
    }
     if (array instanceof Object) {
        if (argc === 1 || argv[1] === 'CASE_LOWER' || argv[1] === 0){
            case_fn = "toLowerCase";
        } else{
            case_fn = "toUpperCase";        }
        for (key in array) {
            tmp_ar[key[case_fn]()] = array[key];
        }
        return tmp_ar;    }
 
    return false;
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
array_change_key_case(42);

Should return

1
false

» Example 2

Running

1
array_change_key_case([ 3, 5 ]);

Should return

1
{0: 3, 1: 5}

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_change_key_case 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
Kevin van Zonneveld
8 Jan '09 Permalink

q  @ Scot Diddle: PS, may I ask to what purpose you are building this page? If it is for testing purposes you may want to look into our testsuite which runs from commandline (mac & linux).

Gravatar
Kevin van Zonneveld
8 Jan '09 Permalink

q  @ Scot Diddle: Our testsuite does not produce any unexpected results with the example-based test-cases. Currently it is not clear to me what statement exactly fails.

If the question is just how to get a value from an object-element:
[CODE="Javascript"]
for (a in array_change_key_case_query) {
// a is the key
val = array_change_key_case_query[a];
// val is now the value of the element with key: a
}
[/CODE]

Gravatar
Scot Diddle
8 Jan '09 Permalink

q  Hi,

I am building a PHP/JS page to call, process, and display the results of eache php.js function.

I'm not sure why I am getting back '[object object]' for array_change_key_case();

[CODE="php"]

/**
*
* array_change_key_case()
*
*/

$integer = 42;

$simpleArray = '[ 3, 5 ]';

$associativeArrray = "{ FuBaR: 42, Dry: 'Do not repeat repeat yourself' } ";

$associativeArrrayForDisplay = "{ FuBaR: 42, Dry: \'Do not repeat repeat yourself\' } ";
[/CODE]


		var answer = confirm("Show: array_change_key_case() ?");

		if (answer) {
			
			var array_change_key_case_query = array_change_key_case(<?php echo $integer ?>);   
			alert('array(<?php echo $integer; ?>)  : ' + array_change_key_case_query);
			
			var array_change_key_case_query = array_change_key_case(<?php echo $simpleArray ?>);   
			alert('array(<?php echo $simpleArray; ?>)  : ' + array_change_key_case_query);
						
			var array_change_key_case_query = array_change_key_case(<?php echo $associativeArrray ?>);   
			
			for (a in array_change_key_case_query) {
			
				alert('array(<?php echo $associativeArrrayForDisplay; ?>)  : ' + a);
				
			}

		}



The "For (a in Oject) returns the Index in lower case, but where did the value(s) associated with the new lowercase index go... How do you use the output from this function. ?

Thanks, Scot L. Diddle

Gravatar
Kevin van Zonneveld
17 May '08 Permalink

q  @ d3x: Awesome!!!! Added.

Gravatar
d3x
17 May '08 Permalink

q  Javascript equivalent for the PHP array():

[CODE=&quot;Javascript&quot;]
function array(){
return Array.prototype.slice.call(arguments);
}
[/CODE]


Contribute a New function