for loop repeating in for each loop - php

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.

Related

Max Amount of Loops

i have loops script like this
for($i=0; $i < count($json); $i++) {
}
for example, amount of $json is "12" or anything more than 10, but i want the max of that loops is 10, but, if i use this script
for ($x = 0; $x < 10; $x++) {
}
the result will be 10, but what if the $json amount I got is less than 10? means there will be NULL results, is there any suggestion?
You can use min() (http://php.net/manual/en/function.min.php)
$count = min (count($json), 10);
for($i=0; $i < $count; $i++) {
}
It's best to do the min outside the for so that it's only done once.
$count=count($json);
if($count > 10){
$count = 10;
}
for ($x = 0; $x < $count; $x++) {
}
You can use array_slice to grab the first 10 of the array and foreach them.
This way you will always just loop 10 times at the most.
$json =[1,2,3,4,5,6,7,8,9,10,11,12,13,14];
//$json =[1,2,3,4,5,6]; //uncomment if you want to test with smaller array
$arr = array_slice($json, 0,10);
Foreach($arr as $val){
Echo $val ."\n";
}
This can also be written like this:
Foreach(array_slice($json, 0,10) as $val){
Echo $val ."\n";
}
But I spelled it out just to make it clear.
You can try the code here: https://3v4l.org/7pV3X

Change foreach loop to a for loop

I'm quite new to PHP. How can I change this foreach loop to only loop twice?
<?php
foreach($results as $row):
?>
Try this code, you just need a counter variable, initialize your counter variable from zero and increment it after each iteration.
<?php
$counter = 0;
foreach($results as $row) {
//your code
if($counter == 1)
{
break;
}
$counter++;
}
?>
You could iterate up to the size of the array or 2 - the smaller of the two.
<?php
$maxInd = min(2, count($results);
for ($i = 0; $i < $maxInd; ++$i) {
$row = $results[$i];
// Do something interesting with $row
}
?>
<?php
for ($i = 0, $count = count($results); $i < $count; ++$i) {
var_dump($results[$i]);
// or assign it to $result variable
$result = $results[$i];
}
?>
But this will work only for collection there are some examples for example generators where you must use foreach.

How to return the object value within for loop?

Okay so i am trying to retrieve the object value iterating through a for loop.
Manually it looks like this:
echo $game->stats->item0;
echo $game->stats->item1;
...
I want to do it something like this:
for($i = 0; $i < 6; $i++) {
echo $game->stats->item.$i;
}
The above however just returns the value of $i. How can i return the actual object value?
Thanks
Basically, your statement should be like this:
echo $game->stats->{'item'.$i}
Regards,
Instead of item.$i, use {"item$i"}.
for($i = 0; $i < 6; $i++) {
echo $game->stats->{"item$i"};
}
Another way you can do it is to set a variable to be equal to 'item' . $i.
for($i = 0; $i < 6; $i++) {
$item = 'item' . $i;
echo $game->stats->$item;
}

PHP Array, Get every 4 results and output in a loop

I'm trying to output my array results in groups of 4.
<?php for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } ?>
The above does 4, but obviously doesn't re-loop.
You can loop whole array and group you output with help of "%" operator.
<div>
<?php for ($i = 0; $i < count($array); $i++) {
if (($i % 4) == 0) {
echo "</div><div>";
}
echo "Element " . $array[$i]; // CODE
}
</div>
Other than using Mod as the other answers show, you could use array_chunk() to create the groups:
$groups = array_chunk($original_array, 4);
foreach($groups as $group){
echo '<div>';
foreach($group as $item){
echo $item;
}
echo '</div>';
}
You can use a while loop to reloop for the whole results to be printed
<?php while(conditions) {
for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } } ?>
Try this that way you can jump by 4
for ($i = 0; $i < 20; $i = $i+4) {
echo $i.'<br/>';
}
I would use a foreach and then just throw in an extra check to output the divs.
$i=0;
foreach ($array as $key->$val)
{
if($i%3==0)
{
echo "<div>";
}
// your stuff
if($i%3==0)
{
echo "</div>";
}
$i++;
}
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
you can check out from here http://php.net/manual/en/function.array-slice.php
try this, use nested for loop, this will loop 4 times. You can try to integrate with your code. If
for ($i = 0; $i < 4; $i++){
for($j = 0; $j < 4; $j++){
echo $a[$j++];
}
echo "<br/>";
}
I hope it can help you.
you can try $i++, because you use ++$i in this way "for" works 3 times!
for ($i = 0; $i < 4; $i++)

Increment a value inside php foreach?

Is it possible to increment a php variable inside a foreach?
I know how to loop by declaring outside.
I'm looking for something like the syntax below
foreach ($some as $somekey=>$someval; $i++)
{
}
No, you will have to use
$i = 0;
foreach ($some as $somekey=>$someval) {
//xyz
$i++;
}
foreach ($some as $somekey=>$someval)
{
$i++;
}
lazy way of doing is:
{
$i=1;
foreach( $rows as $row){
$i+=1;
}
}
,but you have to scope foreach for $i to dont exist after foreach or at last unset it
$i=1;
foreach( $rows as $row){
$i+=1;
}
unset($i);
, but you should use for cycle for that as leopold wrote.
$dataArray = array();
$i = 0;
foreach($_POST as $key => $data) {
if (!empty($data['features'])) {
$dataArray[$i]['feature'] = $data['features'];
$dataArray[$i]['top_id'] = $data['top_id'];
$dataArray[$i]['pro_id'] = $data['pro_id'];
}
$i++;
}
I know it is an old one here, but these are my thoughts to it.
$some = ['foo', 'bar'];
for($i = 0; $i < count($some); $i++){
echo $some[$i];
}
-- Update --
$some = ['foo', 'bar'];
$someCounted = count($some);
for($i = 0; $i < $someCounted; $i++){
echo $some[$i];
}
It would achieve, what you are looking for in first place.
Yet you'd have to increment your index $i.
So it would not save you any typing.
Is there any reason not to use
foreach ($some as $somekey=>$someval)
{
$i++;
}
?
foreach ($some as $somekey=>$someval)
{
$i++;
}
foreach ($some as $somekey=>$someval)
{
$i++;
}
i is just a variable. Even though it's used to iterate over whatever item you're using, it can still be modified like any other.
This will do the trick!
Remember that you'll have to define $i = 0 before the foreach loop if you want to start counting/incrementing from 0.
$i = 0;
foreach ($some as $somekey=>$someval) {
$i++;
}
Unfortunately, this is not possible. I was looking for an elegant way to do this, very similar to this:
for ($k = 0 ; $k < $columns; $k++){ echo '<td></td>'; }
This works for the for functionality in PHP, but then you would have to pull the item, such as $columns[$k], and then you're back to a less than ideal situation.
Reference

Categories