0

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 ?

2
  • next($key)? did you tried that Commented May 13, 2015 at 19:49
  • I don't know exactly what you want to do but if you want the next value in the array you are currently looping through, I think you would need to call next on that array. So... next($this->pages[0]) Commented May 13, 2015 at 19:54

2 Answers 2

1

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

continue;
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

Thanks a lot! But what if i have more elements in array and need to stop foreach after moved poiner to next element ? dump($val) return me all enements in array exept $key == 2 ?
So you want to stop the loop entirely?
Yes, becouse I'd like to reach some next element in array depends on changing condition.
I resolved it by: foreach ($this->pages[0] as $key => $val){ if ($key == $_GET['page_id']){ $myCond = true; continue; } if ($myCond == true) { dump(($val)); break; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.