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 805115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:01:54+00:00 2026-05-15T00:01:54+00:00

I have a bit of an array headache going on. The function does what

  • 0

I have a bit of an array headache going on. The function does what I want, but since I am not yet to well acquainted with PHP:s array/looping functions, so thereby my question is if there’s any part of this function that could be improved from a performance-wise perspective?

$var = myFunction ( array('key1', 'key2', 'key3', '111') );

function myFunction ($keys) {
    $prefix = 'prefix_';

    $keyCount = count($keys);

    // Prefix each key and remove old keys
    for($i=0;$i<$keyCount; $i++){
        $keys[] = $prefix.$keys[$i];
        unset($keys[$i]);
    }
    // output: array('prefix_key1', 'prefix_key2', 'prefix_key3', '111)

    // Get all keys from memcached. Only returns valid keys
    $items  = $this->memcache->get($keys);
    // output: array('prefix_key1' => 'value1', 'prefix_key2' => 'value2', 'prefix_key3'=>'value3)
    // note: key 111 was not found in memcache.

    // Fill upp eventual keys that are not valid/empty from memcache
    $return = $items + array_fill_keys($keys, '');
    // output: array('prefix_key1' => 'value1', 'prefix_key2' => 'value2', 'prefix_key3'=>'value3, 'prefix_111' => '')

    // Remove the prefixes for each result before returning array to application
    foreach ($return as $k => $v) {
        $expl = explode($prefix, $k);   
        $return[$expl[1]] = $v;
        unset($return[$k]);
    }

    // output: array('key1' => 'value1', 'key2' => 'value2', 'key3'=>'value3, '111' => '')

    return $return;

}

Thanks a lot!

Edit: Requested psuedo-code:

  1. Add prefixes to array, since we
    need to prefix every key to prevent
    kes being overwritten in memcache
  2. Get all keys from memcache
  3. Fill up eventual keys that are not valid,
    since we would like to avoid any
    “not-valid-index” errors caused by
    the fact of a requested key not
    returned.
  4. Remove the prefixes to make formatting the outputted keys
    easier without having to get prefix
    for each value.
  • 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-15T00:01:55+00:00Added an answer on May 15, 2026 at 12:01 am

    Well, personally I don’t like modifying an array within a loop (unsetting, etc). You can do it, but how I would do it (just my style) would be:

        function myFunction(array $keys) {
            $prefixedKeys = array();
            $prefix = 'prefix_';
            //Since we want the original key later, create a new array of prefixed keys
            foreach ($keys as $key) {
                $prefixedKeys[] = $prefix . $key;
            }
    
            $data = $this->memcache->get($prefixedKeys);
    
            $return = array();
            foreach ($keys as $key) {
                $prefixedKey = $prefix . $key;
                //Use the cached key if possible, otherwise default to ''
                if (isset($data[$prefixedKey])) {
                    $return[$key] = $data[$prefixedKey];
                } else {
                    $return[$key] = '';
                }
            }
            return $return;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 495k
  • Answers 495k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I don't know if there is a way to modify… May 16, 2026 at 11:23 am
  • Editorial Team
    Editorial Team added an answer Not that I've ever touched Spring, but presumably: title="<spring:message code="toolTipEnglish"/>"… May 16, 2026 at 11:23 am
  • Editorial Team
    Editorial Team added an answer It looks you are fetching data from some different host.… May 16, 2026 at 11:23 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have an issue in flex which is causing a bit of a headache!
I have a bit array implementation where the 0th index is the MSB of
I have a data stream that is addressable only in 8-bit bytes, I want
I have the following code (I know that this code is not optimized but
Is there an easy way to marshal a PHP associative array to and from
I have an information retrieval application that creates bit arrays on the order of
I was working with bit shift operators (see my question Bit Array Equality )
I have a cube which I want to rotate. I also have a light
I have 3 arrays coming from a webservice... Countries : Consists of Key -
I've found some answers that are closely related to this problem, but I still

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.