2013-05-15 134 views
1
$records = array(
'123PP' => 3.63, 
'123DDD' => 9.63, 
'123D' => 6.63, 
'123PPPP' => 9.63, 
'123DD' => 9.63, 
'123P' => 2.63, 
'123PPP' => 1.53 
); 

设置的寡核苷酸序列的优先顺序通过记录循环后,我只得到一个价值 其关键应该是123D因为偏好顺序是: 123D123P123DD123PP123DDD123PPP123PPPP ...PHP中使用字符串长度

对于如:

  • 如果在数组中找不到,那么123P就是答案。
  • 如果在该数组中找不到123P,则返回123DD即可。

而且我已经找到了解决办法:

foreach ($records as $key => $value) { 
if (empty($this->minLength)) { 
$this->invoiceTax = $value; 
      $this->minLength = strlen($key); 
     } 
     elseif (strpos($key, 'P') !== false && (strlen($key) < $this->minLength)) { 
      $this->invoiceTax = $value; 
      $this->minLength = strlen($key); 
     } 
     elseif (strpos($key, 'D') !== false && (strlen($key) <= $this->minLength)) { 
      $this->invoiceTax = $value; 
      $this->minLength = strlen($key); 
     } 

但我想知道如果这个代码可以通过不存储每一个关键的字符串长度进行优化。

+0

使用select然后ctrl + k代替br代码块 –

回答

0

这个函数很容易被整理,但这是可以通过递归来解决的。这意味着,如果123D是在阵列中的代码将被高度优化并且仅用于对123P123DD运行一次,两次,三次等

function GetMostPref($records, $key = "123", $next = "D", $depth = 0) 
{ 
    if($depth == count($records)) 
    { 
     // Hit end of array with nothing found 
     return false; 
    } 

    if(strpos($next, 'D') !== false) 
    { 
    // Currently looking at a 'D...' key. 
    // Next key is the same length as this key just with Ps. 
    $nextKey = str_repeat('P', strlen($next)); 
    } 
    else if(strpos($next, 'P') !== false) 
    { 
    // Currently looking at a 'P...' key. 
    // Next key has one extra char and is all Ds. 
    $nextKey = str_repeat('D', strlen($next)+1); 
    } 
    else 
    { 
     // Key not valid 
     return false; 
    } 

    if(array_key_exists($key.$next, $records)) 
    { 
     // Found the key in the array so return it. 
     return $records[$key.$next]; 
    } 
    else 
    { 
     // Recursive call with the next key and increased depth. 
     return GetMostPref($records, $key, $nextKey, $depth + 1); 
    } 
} 


// Testing 
$records = array(
'123PP' => 3.63, 
'123DDD' => 9.63, 
'123D' => 6.63, 
'123PPPP' => 9.63, 
'123DD' => 9.63, 
'123P' => 2.63, 
'123PPP' => 1.53 
); 

// Finds 123D and returns 6.63 
echo GetMostPref($records); 
+0

感谢您的回复。但让我详细解释一下这个案例。 $ records是一个多维的记录,其中$ records [$ i] [123P]的值为2.63,其中$ i的范围可以在0 - 100之间。所以,我认为传递整个数组和array_key_exists是无济于事的。 –

+0

应该仍然可以,只需要重新编写代码,使它指向'$ records'的右侧部分。如果你担心传递太多,你可以使用'$ records'指针。 – MatthewMcGovern

0
function prepareFunctionCall(){ 
    $records = array('123PP' => 3.63,'123DDD' => 9.63,'123PPPP' => 9.63 
    ,'123DD' => 9.63,'123P' => 2.63,'123PPP' => 1.53); 

    // '123D' => 6.63, 
    foreach($records as $key=>$value){ 
    $tmp = strlen($key).$key; 
    $arTmp[$tmp] = $value; 
    } 
    getFirstValue($arTmp); 
} 

function getFirstValue($pArray){ 
    ksort($pArray); 
    reset($pArray); 
    print key($pArray).' = '.current($pArray); 
} 

这是提供了良好的解决方案的替代通过MatthewMcGovern。

我提供了替代方案,因为这个函数使用了php函数ksort,reset和current。这些功能是为这种情况创建的,如果可能的话,我会建议您在找出哪个键是第一个要选择的键之前重新编写数组的键。这就是我在增加strlen时所做的。但与收集数据时重写密钥相比,这并不理想。函数的核心是对函数ksort,reset和current的调用。