简体   繁体   中英

How to get next element in array

When I dump my array I revieve records. I'd like to stop forear if condition is met and move index to the next element but I dont understand something. I use:

    foreach ($this->pages[0] as $key => $val){
        if ($key == 2){
            dump($val);
        }   
    }

after dump($this->pages[0]) I recieve array

Array
(
    [2] => Array
        (
            [id] => 2
            [name] => Wstęp
            [symbol] => wstep
        )
    [5] => Array
        (
            [id] => 5
            [name] => Prezentacja spółki
            [symbol] => prezentacja-spolki
        )
)

dump($val) returns

Array
    (
        [id] => 2
        [name] => Wstęp
        [symbol] => wstep
    )

an the problem is when I try move to next element in array using

dump(next($val));

returns

Prezentacja spółki

Hov to properly move to pointer to naxt element in array ?

In a foreach loop you use continue to stop the current element process and advance to the next element

foreach ($this->pages[0] as $key => $val){
    if ($key == 2){
        continue; // Go to the next element
    }   
}

To stop the loop entirely, use break instead of continue

If you want the foreach loop to skip directly to the next element. you can use:

continue;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM