Converting keys of an array/object-tree to lowercase
Posted
by tstenner
on Stack Overflow
See other posts from Stack Overflow
or by tstenner
Published on 2010-06-05T18:07:23Z
Indexed on
2010/06/05
18:12 UTC
Read the original article
Hit count: 175
php
|Performance
Im currently optimizing a PHP application and found one function being called around 10-20k times, so I'd thought I'd start optimization there.
function keysToLower($obj)
{
if(!is_object($obj) && !is_array($obj)) return $obj;
foreach($obj as $key=>$element)
{
$element=keysToLower($element);
if(is_object($obj))
{
$obj->{strtolower($key)}=$element;
if(!ctype_lower($key)) unset($obj->{$key});
}
else if(is_array($obj) && ctype_upper($key))
{
$obj[strtolower($key)]=$element;
unset($obj[$key]);
}
}
return $obj;
}
Most of the time is spent in recursive calls (which are quite slow in PHP), but I don't see any way to convert it to a loop. What would you do?
© Stack Overflow or respective owner