I have an array that gets processed through a foreach loop.
foreach($images as $imageChoices) {
Do stuff here
}
How do I limit the loop to only process the first 4 items in the array?
The array_slice() function can be used.
foreach(array_slice($images, 0, 4) as $imageChoices) { … }
This enables you to only loop over the values that you want, without keeping count of how many you have done so far.
You basically count each iteration with the $i and stop the loop with break when 4 is reached...
$i = 0;
foreach($images as $imageChoices) {
//Do stuff here
$i++;
if($i >= 4 { break; }
}
You can do:
for($i=0; $i<count($images) && $i<4; $i++) {
// Use $images[$i]
}
Use a counter variable and increase it's count on each loop.
Apply check according to counter value
something like below:
$count=1;
foreach($images as $imageChoices) {
if($count <=4)
{
Do stuff here
}
else
{
Jump outside of loop break;
}
$count++;
}
OR you can do same with for loop instead of foreach and also with some inbuilt PHP Array functions
for($i=0; $i<4; $i++) {
// Use $images[$i]
}
function process()
{
//Some stuff
}
process(current($imageChoices));
process(next($imageChoices));
process(next($imageChoices));
process(next($imageChoices));
Related
I want the function to generate random number 5 times. I have already tried to use do/while loop but it doesn't work:
function myfunction(): int {
$i = 0;
do {
$k = mt_rand(10000, 99999);
$i++;
} while($i <= 4);
{
return $k;
}
}
Stack your values in an array, inside your do-while loop and just before your function ends add the return of the array.
Something like this :
function myfunction():array{
$randomNumbers = array();
$i=0;
do{
$k=mt_rand(10000,99999);
$i++;
$randomNumbers[] = $k;
} while($i<=4) ;
return $randomNumbers;
}
print_r(myfunction());
The syntax of do while loop is given below...
do {
code to be executed;
} while (condition is true);
You have to update your code as it is given below...
do{
$k=mt_rand(10000,99999);
$i++;
}while($i<=4);
I have modified the code a little bit and tested it before sending it to you . Please check the following code :-
function fiveRandomNos(){
$rno = array();
$count=0;
while($count <= 4){
$rno[]=mt_rand(10000,99999);
$count++;
}
return $rno;
}
print_r(fiveRandomNos()) ;
I am having for loop in foreach loop
static $count=0;
for($i=$count;$i<$semesters_count;$i++)
{
echo $array_wam[$i];
$count++;
}
here $array_wam is array of some marks. I am printing multiple student marks
I getting first student marks
first student
50.6
second student
50.6 60.9
I want to show output like
first student
50.6
second student
60.9
Here loop again starts with 0 but I want loop starts with where it ends.
I dont know if i am understanding right what you are asking for but have you tried like this?
for($i=0;$i<$semesters_count;$i++){
echo $array_wam[$i];
}
For that you have to put static $count=0; before the foreach starts.
See an example below:
static $count=0;
foreach ($item)
{
for($i=$count;$i<$semesters_count;$i++)
{
echo $array_wam[$i];
$count++;
}
}
The problem is that you're not incrementing $count when you reach the end of the for loop.
Instead of incrementing $count inside the loop, set it to $i at the end of the loop:
static $count = 0;
for ($i = $count; $i < $semesters_count; $i++) {
echo $array_wam[$i];
}
$count = $i;
Try this :-
static $count = 0;
foreach ($item)
{
$semester_count_total = $semesters_count + $count;
for ($i = $count; $i < $semester_count_total; $i++) {
echo $array_wam[$i];
}
$count = $semester_count_total;
}
Try this:
for($i = 0; $i < $count_semester; $i++){
echo $array_wam[$i];
}
Why do you want static $count if it can loop trough for loop? And it still counters your $i variable is equal to the length of your $count_semester.
So I have a for loop that is decreasing...
for ($i=count($array); i>0; $i--;)
{
if(condition)
{DO SOMETHING like print the element in a decreasing manner}
if(enter ending iteration condition here after xth element) break;
}
that pretty much sums up my question. How do I formulate the ending iteration - let's say after 5 elements printed I want to stop the iteration.
$j = 0;
for ($i=count($array); $i>0; $i--)
{
if(condition)
{
DO SOMETHING like print the element in a decreasing manner;
$j++;
}
if($j > 4){
break;
}
}
Try to reverse the count of the loop. Instead of decreasing, try to increase so you will have a count of how many items are being printed.
<?php
for ($i = 0; $i < count($array); $i++)
{
if(condition)
{
/* DO SOMETHING like print the element in a decreasing manner */
}
/* replace (nth) with the needed number */
if($i === (nth)) break;
}
You could set the limit based on the count, like:
$loop_limit = 5;
$array_count = count($array);
$last = $array_count - $loop_limit;
for ($i = $array_count; $i >= $last ; --$i) {
if ( $i == $last ) {
//Do whatever you need at this point
}
//do the normal loop action
}
I tried to use mysqli_fetch_array twice in a code, and it seems not to work.
<?php
$i=1;
while ($i < $x)
{
while($data=mysqli_fetch_array($result))
{
echo $i;
echo $data['ID'];
}
}
while {$i > $x)
{
while($data=mysqli_fetch_array($result))
{
echo $i;
echo $data['ID2']
}
}
?>
This doesn't work for the second case. Why? Is there any way to get my idea to work?
I want to use some of the array values in one place, and some in another
After you fetch all rows the pointer is at the end of the result set and you cannot fetch more. You need to reset it to the first row before attempting to fetch from it again:
mysqli_data_seek($result, 0);
Alternately, the first time through assign rows to an array and use that later:
while($data=mysqli_fetch_array($result)) {
$rows[] = $data;
}
foreach($rows as $row) {
//whatever
}
Depending on what you are doing not a great way to split up rows, but based on your comments (you have to increment your counter):
$i=1;
while($data=mysqli_fetch_array($result) && $i < 11) {
echo $data['ID'];
$i++;
}
while($data=mysqli_fetch_array($result) && $i < 21) {
echo $data['ID2'];
$i++;
}
Where are you resetting the vars, also you should do the while loop like this:
while ( ($i < $x) || ($data=mysqli_fetch_array($result) ) )
Say i start my counter at 400. How would i execute a foreach loop that will run backwards until 0?
pseudocode
$i = 400;
foreach(**SOMETHING**)){
//do stuff
$i--;
}
for($i = 400; $i > 0; $i--)
{
// do stuff
}
other ways to do it:
$i = 400;
while($i > 0)
{
// do stuff
$i--;
}
or
$a = range(400, 1);
foreach($a as $i)
{
// do stuff
}
In case you really want to iterate backwards over an existing array you can use array_reverse():
foreach(array_reverse($myArray) as $myArrayElement){
// do stuff with $myArrayElement
}
how about a for loop
for($i = 400; $i > 0; $i--)
{
//stuff
}
foreach is used for iterating over sequences or iterators. If you need a conditional loop then use for or while.