开发者

PHP modifying and combining array

开发者 https://www.devze.com 2022-12-31 22:49 出处:网络
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

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',开发者_JS百科 '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.


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


You can replace this:

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

with this:

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

I don't think you actually wanted unset($keys[$i]), as that just undoes the concatenation.


Ok, full 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消