简体   繁体   English

PHP修改和组合数组

[英]PHP modifying and combining array

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? 该函数做了我想要的,但由于我还不熟悉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;

} }

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 将前缀添加到数组,因为我们需要为每个键添加前缀以防止在memcache中覆盖kes
  2. Get all keys from memcache 从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. 我不认为你真的想要unset($keys[$i]) ,因为它只是撤消连接。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM