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);
for($i=0;$i<$keyCount; $i++){
$keys[] = $prefix.$keys[$i];
unset($keys[$i]);
}
$items = $this->memcache->get($keys);
$return = $items + array_fill_keys($keys, '');
foreach ($return as $k => $v) {
$expl = explode($prefix, $k);
$return[$expl[1]] = $v;
unset($return[$k]);
}
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.
- .