2013-05-15 133 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的調用。