PHP modification and array concatenation

I have a bit of a headache array. The function does what I want, but since I am still not familiar with the PHP functions: s array / looping, so, therefore, my question is whether there is any part of this function that can be improved with performance point of view?

$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;

}

Thank you so much!

Edit: requested psuedo code:

  • Add prefixes to the array, since we need to prefix each key to prevent kes from being overwritten in memcache
  • Get all keys from memcache
  • Fill in the possible keys that are invalid, because we would like to avoid any "invalid index" errors caused by the fact that the requested key has not returned.
  • .
+3
3

, ( ..). , ( ):

    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;
   }
+2

:

for($i=0;$i<$keyCount; $i++){
    $keys[] = $prefix.$keys[$i];
    unset($keys[$i]);
}

:

foreach($keys as &$key){
    $key = $prefix.$key;
}
unset($key);    //Necessary because the reference needs to be destroyed

, unset($keys[$i]), .

+1

Ok, complete solution:

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

    //Create an array to hold 'prefixedkey' => 'key' pairs
    $prefixedkeys = array();
    foreach($keys as $key)
    {
        $prefixedkeys[$prefix.$key] = $key;
    }

    //Pass memcache just the prefixed keys
    $items = $this->memcache->get(array_keys($prefixedkeys));

    //Create an array to hold the final results
    $return = array();

    foreach($prefixedkeys as $prefixedkey => $key)
    {
        if(!isset($items[$prefixedkey]))
        {
            //If the memcache data is not set for the current prefixed key,
            //set the non-prefixed key in $return to ''
            $return[$key] = '';
        }
        else
        {
            //Otherwise, set it to the memcache data for the current prefixed key
            $return[$key] = $items[$prefixedkey];
        }
    }
    return $return;
}
+1
source

Source: https://habr.com/ru/post/1746748/


All Articles