2019-07-12
How to Restart a loop from first in php
stackoverflow
Question

Here I have 2 machines I need to Allocate products for those machines. Each machine has the 50 products capacity, so if I have 100 products, first 50 to the first machine and second 50 to the second machine, the above concept is working fine, but if my products count is 150, then the allocation should be first 50 to first machine , second 50 to Second machine, then the third 50 to First machine ,

$CountMachine=2;
$balanceQty=150;
$Loopcount=1;
foreach($FetchMachine as $i => $row){ //array has 2 records
     //allocated 50 products
     $balanceQty=$balanceQty-50;
     if($Loopcount==$CountMachine && $BalanceQty>0){
   $i=0; //herei got stuck , how to restart the loop from here, if the balance quantity is greater than 0 but loop excution is completed
  }
    }
$Loopcount++;
Answer
1
 $CountMachine=2;
 $balanceQty=150;
 while($balanceQty>0){
 foreach($FetchMachine as $i => $row){ //array has 2 records
      //allocated 50 products
      $balanceQty=$balanceQty-50;

     }
     }
How to Restart a loop from first in php
See more ...