Re: Q: while loop on an array

From: Date: Wed, 23 Jan 2002 15:42:40 +0000
Subject: Re: Q: while loop on an array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-81695@lists.php.net to get a copy of this message
Usually, you would want to use a count variable in your while loop, or to simplify things, you could use a for loop... Example: $arr = array(1, 2, 7, 9, 3); $arr_size = count($arr); for($i = 0; $i < $arr_size; $i++){ /* body of loop here */ $j = $arr[$i]; } this starts off with $i = 0, and increments it at the end of each loop. As soon as $i is incremented past the last element in the array, the check $i < $arr_size will come up false, and so the loop will not be entered any more. You can do the same thing in a while loop by intializing a variable to 0 before the loop, and have a statment in the body of the loop that increments it each time... Hope that helps. Jeff ----- Original Message ----- From: "Joe Rice" <jrice@bigidea.com> To: <php-general@lists.php.net> Sent: Wednesday, January 23, 2002 7:17 AM Subject: [PHP] Q: while loop on an array > > Hello, > > I would like to know how i can know when i'm at the > end of an array during a while loop? > > reset($new_answers); > while (list($new_qID,$new_aID) = each ($new_answers)) { > $sql .= "qID=\"$new_qID\ ||" "; > } > $sql .= ") AND uID = \"$uID\""; > > i would like to know how to stop putting the " || " at > the end of $sql in the middle of the while loop. > > any help would be appreciated. > > thanks, > joe

« previous php.general (#81695) next »