2015-10-26 41 views
0

所以我們的目標是能夠根據數據庫中的動態變量爲我的進度條創建一個動態值。下面是我所期待的,如果可能的僞代碼:比較數組值來計算PHP中的%?

//League rank names 
['bronze_V','bronze_IV','bronze_III','bronze_II','bronze_I']; 

Lets say i start at bronze_IV to bronze_II i need to calculate that to 100? 
and then i need to store that value is that possible to be dynamic? 
+1

是的,這可以被完成,不是沒有你提供的片段。如果你需要幫助,你需要提供更多信息。數字數據在哪裏? – Christian

回答

1

你想這樣的事情,

$data = ['bronze_V','bronze_IV','bronze_III','bronze_II','bronze_I']; 

$start = $end = 0; 

while($element = current($data)) { 
    if ($element == 'bronze_IV') $start = key($data); 
    if ($element == 'bronze_I') $end = key($data); 
    next($data); 
} 

$percentage = ((($end-$start) + 1)/count($data)) * 100; 

var_dump($percentage); //float(80) 

例如多層次,

#note : badge count for each tier has to be 5 for this to work, 
#if you change the count change change the value inside the loop 

$data = array(
     ['silver_V','silver_IV','silver_III','silver_II','silver_I'], 
     ['bronze_V','bronze_IV','bronze_III','bronze_II','bronze_I'] 
    ); 

$start = $end = 0; 

$start_badge = 'silver_V'; 
$end_badge = 'bronze_II'; 

//loop through tiers 
foreach ($data as $key => $tier) { 
    //loop through badges 
    while($badge = current($tier)){ 
     //position of the start badge 
     if ($badge == $start_badge) $start = ($key * 5) + key($tier)+1; //see note 
     //position of the end badge 
     if ($badge == $end_badge) $end = ($key * 5) + key($tier)+1; //see note 
     next($tier); 
    } 
} 

//count badges 
$badges_count = (count($data, COUNT_RECURSIVE) - count($data)); 

//calculate percentage 
$percentage = ((($end-$start) + 1)/$badges_count) * 100; 

var_dump($percentage); //float(90) 
+0

這看起來很有希望的生病試試吧 – Elevant

+0

你能否重申一下我將如何修改上述內容以包含整個聯盟層級的青銅,銀,金,鉑和鑽石。 請評論我的每一部分感謝:) – Elevant

+0

@Elevant是那些不同的陣列,每個有5個級別? –