Count the percentage of the number of the current iteration in a foreach loop

后端 未结 5 2006
别那么骄傲
别那么骄傲 2021-01-29 07:59

I am trying to build up a script that gets the current percentage of the iteration of a loop.

I have :



        
5条回答
  •  囚心锁ツ
    2021-01-29 08:37

    you need to do ($total/$iterations) * $counter like this code:

     $counter = 0;
     $total = 100;
     $iterations = count($key);
    
     foreach($key as value) {
          $counter++;
          $percentage = (($total/$iterations) * $counter)/100;
     }
    

提交回复
热议问题