I want to limit a foreach loop to a number of loops. How I can this be done in PHP?
We'll need help from this PHP function:
array_slice to accomplish this task.
Let's say we have an array
$boxes which contains five elements. We want to process only three of them.
// We have boxes of fruits
$boxes = array(
array('label' => 'apples'),
array('label' => 'oranges'),
array('label' => 'bananas'),
array('label' => 'grapes'),
array('label' => 'watermelons'),
// Show only the first three boxes
foreach(array_slice($boxes, 0, 3) as $box)
// Print box labels
Above code will output: