Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 828639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:45:19+00:00 2026-05-15T03:45:19+00:00

I am currently optimizing a PHP application and found one function being called around

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:45:20+00:00Added an answer on May 15, 2026 at 3:45 am

    Foreach is using an internal copy that is then traversed. Try it without:

    function keysToLower($obj)
    {
        $type = (int) is_object($obj) - (int) is_array($obj);
        if ($type === 0) return $obj;
        reset($obj);
        while (($key = key($obj)) !== null)
        {
            $element = keysToLower(current($obj));
            switch ($type)
            {
            case 1:
                if (!is_int($key) && $key !== ($keyLowercase = strtolower($key)))
                {
                    unset($obj->{$key});
                    $key = $keyLowercase;
                }
                $obj->{$key} = $element;
                break;
            case -1:
                if (!is_int($key) && $key !== ($keyLowercase = strtolower($key)))
                {
                    unset($obj[$key]);
                    $key = $keyLowercase;
                }
                $obj[$key] = $element;
                break;
            }
            next($obj);
        }
        return $obj;
    }
    

    Or use references to avoid that a copy is used:

    function &keysToLower(&$obj)
    {
        $type = (int) is_object($obj) - (int) is_array($obj);
        if ($type === 0) return $obj;
        foreach ($obj as $key => &$val)
        {
            $element = keysToLower($val);
            switch ($type)
            {
            case 1:
                if (!is_int($key) && $key !== ($keyLowercase = strtolower($key)))
                {
                    unset($obj->{$key});
                    $key = $keyLowercase;
                }
                $obj->{$key} = $element;
                break;
            case -1:
                if (!is_int($key) && $key !== ($keyLowercase = strtolower($key)))
                {
                    unset($obj[$key]);
                    $key = $keyLowercase;
                }
                $obj[$key] = $element;
                break;
            }
        }
        return $obj;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've currently got a web application that I need optimizing, and one of methods
I'm currently optimizing an application, one of the operations that is done very often
My Flex web application is almost ready for launch. I'm currently optimizing as much
Currently optimizing an application at work. There are many files in some of the
I have this application written in PHP, I'm considering on using eAccelerator for optimizing
I am currently optimizing a stored procedure, because it times out due to amount
I making an app and im currently optimizing it to Samsung Galaxy Tab. I
Currently, I am writing a MiddleWare application that synchronizes information between and accounting application
Currently I'm starting a new Activity and calling finish on a current one. Is
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.