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

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

    You can do exactly what are you asking in this way

    $counter = 0;
    $length  = count($array);
    
    foreach ($array as $value) {
    
    $counter=$counter+1;
    for ($stepvvx = 10; $stepvvx <= 100; $stepvvx=$stepvvx+10)
        {
    if ($counter==intval(($length*$stepvvx)/100)){
        echo "
    $stepvvx %"; } # do your task here }

提交回复
热议问题