php - Modulus inside loop
I have an array that I am looping through and breaking up into chunks of 50. However occasionally the number of items inside that array are more than what fits inside that chunk of 50 ex.:
$array = array(); // has 220 rows
for ($i = 0; $i < count($array); $i++) {
$j[] = $i;
if ($i % 50 == 1) {
print_r($j); // do something here with the 50 rows
$j = null;
}
}
The problem here is that this will not print anything after201
. I know there is some algebraic math involved in solving this but I am drawing a blank. Its times like these where I really wish I had paid attention in math class back in high school.
Answer
Solution:
I think array_chunk fits up your requirement and no maths required.
Answer
Solution:
Add additional condition
Answer
Solution:
You just have to redeclare the array is my guess:
Once you perform
$j = null
there is no way you can do$j[] = $i
Answer
Solution:
After your loop is finished, you will have the remaining 201 through 220 entries in
$j
, so just do your stuff again.Answer
Solution:
array_chunk might be useful. Basically splits the array into chunks returning a multi dimensional array