PHP loop according to amount of items in array
I'm trying to write a short script that will query my mysql db, and accordin开发者_StackOverflow社区g to the amount of results (dynamic) i want the script on each segment.
For example, $arr
is a result of a mysql_fetch_array
and it has 872 items, I want to run my function 9 times, 1 for each 100 items and the last one for 72 items.
How can I do that?
Simply use a for
loop with an incrementor that increments by 100. You can use array_slice()
to get the concerned rows on each loop.
$dbRows = resultsFromDB();
for($i = 0; $i < count($dbRows); $i+=100) {
$concernedRows = array_slice($dbRows, $i, 100);
mySuperFunction($concernedRows);
}
Maybe something like:
$length = count($arr);
for ($i = 0; $i < ceil($length / 100); $i++) {
}
If I understood.
精彩评论