Is there a "better" way (built-in function, better algorithm) to normalize the case of all the keys in a PHP array? Looping though and creating a new array works

$new = array();
foreach( $old as $key=>$value) {
	$key = strToLower($key);
	if(!array_key_exists($key,$new) {
		$new[$key] = $value;
	else {
		throw new Exception('Duplicate Key Encountered');


but it seems like these should be a way to do this "in place".

Update: It looks like there is a built in, the not deceptively named yet somehow missed by me array_change_key_case. I'd still be interesting in seeing algorithmic approaches that would let you better handle what happens when you hit "duplicate" keys.


For the algorithmic approach I'd go with with soulmerge's "pragmatic" version below.

Written by acrosman

Accepted Answer

I believe array_change_key_case does what you're looking for.

Written by acrosman
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the community.
It is licensed under cc-wiki