Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assigned variable is not working in do while loop in using PHP

I am working on Do While loop in my project its working fine first time.

Before while statement, I assigned a value to an array I could able to print the array successfully at bottom of the code, BUT its become 0 when I check at top of the loop.

Code:

$looparray = array();
$loopend = 0;
$arraymer = array();
$poolafirtsid = $previous_array_values; //previous array values

do {
   if (sizeof($looparray) == 0) {
       $firstsponarray = $poolafirtsid;
   } else {
       $firstsponarray = $looparray;
   }

   $firstsponarray = getUserArray($poolafirtsid);
   //get user arraylist of first

   foreach ($firstsponarray as $avalue) {
       $rooparray = membercount($avalue);
       $bsponarray = getUserArray($avalue);
       //get second users arraylist 9
       if (sizeof($bsponarray > 0)) {
           $barraymer = array_merge($barraymer, $bsponarray);
       }

       $aarraylist[$avalue] = $rooparray;
   }

   $asmallestsponid = getSmallestID($aarraylist);
   //get smallest id in the array

   if (membercount($asmallestsponid) < 3) {
       $loopend = 1;
   } else {
       global $pooldata;
       if (count($barraymer) > 0) {
           $pooldata = $barraymer;
       }
       print_r($pooldata);

   }
} while ($loopend == 1);

When I print in else its working but I am unable to print starting of do loop its showing array size is 0

like image 341
Asesha George Avatar asked Nov 18 '18 20:11

Asesha George


People also ask

Can I declare a variable in a Do While loop?

Yes. you can declare a variable inside any loop(includes do while loop.

Why my do while loop is not working?

The while loop is not run because the condition is not met. After the running the for loop the value of variable i is 5, which is greater than three. To fix this you should reassign the value before running the while loop (simply add var i=1; between the for loop and the while loop).

Does PHP support Do While loop?

PHP do-while loop can be used to traverse set of code like php while loop. The PHP do-while loop is guaranteed to run at least once. The PHP do-while loop is used to execute a set of code of the program several times.

Do While and while loop in PHP?

Different Types of Loops in PHP while — loops through a block of code as long as the condition specified evaluates to true. do… while — the block of code executed once and then condition is evaluated. If the condition is true the statement is repeated as long as the specified condition is true.


1 Answers

I will ignore all the name issues but address your while loop issue:

$loopend =0;
do {
    ...
    if(membercount($asmallestsponid)<3) {
         $loopend = 1;
    }else{
      ...
    }
while ($loopend == 1);

There are 2 options:

  1. The if condition is true: if so, $loopend will get 1 so the loop continue (which not seem fit the call him "loop end" but what ever...)

  2. The if condition is false: then the $loopend stay the same (init as 0) so the loop will stops

IMHO - this will simplify your loop:

do {
    ...
while (membercount($asmallestsponid)<3);
like image 149
dWinder Avatar answered Oct 15 '22 08:10

dWinder