你想這樣的事情,
$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)
是的,這可以被完成,不是沒有你提供的片段。如果你需要幫助,你需要提供更多信息。數字數據在哪裏? – Christian