Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreach break and continue in PHP

Tags:

foreach

php

break

I am facing an problem. I have built a foreach loop in PHP:

 <?php $show = false; ?>
 <?php foreach ($this->items as $item) : ?>

But I want to echo the first 20 items and continue with the following 16 items.

I have managed to make it to do a break after 20 items but I am not getting the following 16 items ( starting from nr 21 ).

This is what I have so far:

<?php $show = false; ?>
<?php $i = $item->id = 1; ?>
<?php foreach ($this->items as $item) : ?>
<?php if(++$i > 20) break; ?>

If I set $i to '21' it still echos item 1 and so on.

Solution ## Thanks to @dhavald

<?php $show = false; ?>                                   
<?php foreach (array_slice ($this->items, 0, 20) as $item) : ?>

By placing an array_slice in the foreach you can control the items you want show. So on the next div i want to show item 21 to 36, the only thing i had to change was the 0 and 20 into , 20, 36

To clarify what I am looking for, I have three divs where I want to echo some items onto it. If an user collapse a div on the first there will appear the first 20 items and if a user clicks div2 the next 16 items will appear.

How can I correct this code to make that happen?

As i forgotten to mention, the solutions you bring on helped me really to understand the process but as i used a component generator for Joomla 3.1 there is one row of code that make it a little bit more complex to call the first 20 and second 16 items.

Directly after the foreach loop there is this line of code

if($item->state == 1 || ($item->state == 0 && JFactory::getUser()>authorise('core.edit.own',' com_ncitycatemus.vraagantwoordtoevoegen.'.$item->id))):

$show = true;

How can i manage it than with the loop ? Or do you suggest another way?

Thanks in advance! Really helped me to understand the loop!!

like image 743
user1629905 Avatar asked May 20 '13 16:05

user1629905


People also ask

Can I use continue in foreach PHP?

Introduction. The continue statement is one of the looping control keywords in PHP. When program flow comes across continue inside a loop, rest of the statements in current iteration of loop are skipped and next iteration of loop starts. It can appear inside while, do while, for as well as foreach loop.

What is difference between break and continue in PHP?

The break statement terminates the whole iteration of a loop whereas continue skips the current iteration. The break statement terminates the whole loop early whereas the continue brings the next iteration early.

What is continue in PHP?

The continue keyword is used to is used to end the current iteration in a for , foreach , while or do.. while loop, and continues to the next iteration.

What is foreach in PHP?

The PHP foreach Loop The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.


1 Answers

break breaks out of and stops a loop. continue skips the rest of the code in the loop, and jumps to the next iteration.

Using break and continue is in my opinion the wrong approach here. I'd simply use two for loops:

$keys = array_keys($this->items);
$keysLength = count($keys);

echo '<div id="div1">';
for ($i = 0; $i < min($keysLength, 20); $i++) {
    echo $this->items[$keys[$i]];
}
echo '</div>';

if ($keysLength >= 20) {
    echo '<div id="div2">';
    for ($i = 20; $i < min($keysLength, 36); $i++) { //36 being 20 + 16
        echo $this->items[$keys[$i]];
    }
    echo '</div>';
}

Note: This will work with any amount of items in $this->items. If it's under 20, it'll simply skip the next 16, and if it's above 36, it'll simply ignore the rest.

like image 184
h2ooooooo Avatar answered Oct 07 '22 01:10

h2ooooooo