【问题标题】:PHP modifying and combining arrayPHP修改和组合数组
【发布时间】:2011-02-22 22:14:03
【问题描述】:

我有点头疼。该函数可以满足我的要求,但是由于我还不太熟悉 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 在内存缓存中被覆盖
  2. 从内存缓存中获取所有键
  3. 填写无效的最终密钥, 因为我们想避免任何 “not-valid-index”错误由 请求密钥的事实不是 返回。
  4. 删除前缀以格式化输出的键 更容易而无需获得前缀 每个值。

【问题讨论】:

  • 你能用伪代码解释一下你想要做什么吗?

标签: php arrays foreach for-loop


【解决方案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;
}

【讨论】:

    【解决方案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;
       }
    

    【讨论】:

      【解决方案3】:

      你可以替换这个:

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

      【讨论】:

      • 其实 unset($keys[$i]) 去掉了原来不带前缀的key?
      • 是的,但这样做是不稳定的,因为键被添加到末尾并从头开始。您将实际的数组键与 表示 键的普通值数组混淆了。 unset($array['key']) 从数组中移除键为 'key' 的元素。
      猜你喜欢
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-03-20
      • 1970-01-01
      相关资源
      最近更新 更多