PHP修改和组合数组

时间:2010-05-23 13:16:54

标签: php arrays foreach for-loop

我有一阵阵头疼的事情发生了。该函数做了我想要的,但由于我还不熟悉PHP:s数组/循环函数,所以我的问题是,如果这个函数的任何部分可以从性能方面改进吗?

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

}

非常感谢!

编辑:请求的伪代码:

  1. 为数组添加前缀,因为我们 需要为每个键加前缀以防止 kes被memcache覆盖
  2. 从memcache获取所有密钥
  3. 填写无效的最终密钥, 因为我们想避免任何 “not-valid-index”错误引起的 请求密钥的事实不是 返回。
  4. 删除前缀以格式化输出的密钥 更容易,无需获得前缀 对于每个值。

3 个答案:

答案 0 :(得分:2)

好吧,我个人不喜欢在循环中修改数组(取消设置等)。你可以做到,但我怎么做(只是我的风格)将是:

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

答案 1 :(得分:1)

你可以替换它:

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]),因为它只是撤消连接。

答案 2 :(得分:1)

好的,完整的解决方案:

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