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

后端 未结 5 2016
别那么骄傲
别那么骄傲 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:42

    To calculate percentage, you take the current and divide it by the total, then multiply that value by 100, then round it off. I also take the floor value so that 99.7% doesn't round up to 100 since it's not truly complete yet.

        for($i=1;$i<=count($yourArray);$i++) {
          $percentage = floor(round( (($i / total) * 100), 1 ));
        }
    

提交回复
热议问题