배열 두통이 계속 발생합니다. 이 함수는 내가 원하는 것을 수행하지만, 아직 PHP : s array/looping 함수에 익숙하지 않기 때문에 퍼포먼스 관점에서 개선 될 수있는이 함수의 부분이 있다면 내 질문이 그 것이다.PHP 수정 및 배열 조합
$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;
}
고마워요!
편집 : 요청 사이비 코드 : 우리가 KES을 방지하기 위해 모든 키를 접두사 필요 이후
- 는, 배열에 접두사를 추가 memcache를 에서 모든 키를 가져옵니다 memcache에
- 에 덮어 쓰기
- 유효하지 않은 최종 키를 채우십시오. "유효하지 않은 색인"오류가 발생했습니다. 요청한 키의 사실은가 아닙니다.이 반환되었습니다.
- 각 값에 대해 접두어 을 가져올 필요없이 출력 된 키의 서식을 지정하기 위해 접두사를 제거하십시오.
당신은 당신이 원하는 정확히 의사 코드로 설명 할 수 ? – Eric
게시물이 업데이트되었습니다. 감사! – Industrial