I am 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});
} elseif (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. How can I do this?