Php foreach step back

Looking foward or back in a foreach loop

Since you are working with, what I assume is categories, I would suggest looking into this: dev.mysql.com/tech-resources/articles/hierarchical-data.html that way your categories are all setup for you via the query etc. A good read either way 🙂

If I understand your problem right, you probably want to use a RecursiveArrayIterator->getDepth and don’t need to do dome next , prev magic.

6 Answers 6

Although I’ve had choppy results with them in the past you can use the below functions to move the current array pointer.

$arr = array(1,2,3,4,5); foreach ($arr as $foo) < if (empty($isrewind)) < reset($arr); $isrewind = true; >echo "node: $foo\n"; $copy = $arr; // make a copy of the array $next = next($copy); echo "next: $next\n"; $copy = $arr; // make a copy of the array $prev = prev($copy); echo "prev: $prev\n"; next($arr); // don't forget to advance the pointer on the original array > 

I’ve demonstrated the prev bit just for the example. You can easily do that without prev() by saving the element at the end of each iteration.

The if empty bit resets the array pointer to the beginning, because foreach will advance the pointer once when it makes a copy of the array.

node: 1 next: 2 prev: node: 2 next: 3 prev: 1 node: 3 next: 4 prev: 2 node: 4 next: 5 prev: 3 node: 5 next: prev: 4 

If you have to do something like this though, there might be a better way just by rearranging your data structure (hard to tell without code).

Источник

Go back at the beginning of a foreach loop in PHP

@panther I have a String array. The initial string is date, like : «28, 29 Fevrier» (in French). And when I meet a month, I would like to save it in a variable, and go back at the beginning of my array -> 28

Читайте также:  Python libraries for gui

@HugoTor: string array? Hm, you explode string to array? If you have string, use regex to get Month and date(s).

5 Answers 5

It is. But not with foreach & without leaving the loop. Here’s another alternative, for good measure.

for ($i = 0; $i < count($array); $i++) < if (condition) < $i = 0; >do_stuff_with($array[$i]); > 

I feel stupid now.. I’m working with foreach loop only, and I don’t thought to the simle for loop, which will work easily (I hope !)

It is not suggested but you can use goto:

Remind me Visual Basic ! Not sure it is the cleaner solution to my ask :/ But if I don’t resolve my problem with @b0s3 function, I’ll try this 🙂

@HugoTor your question has a goto nature by itself. BTW, goto reminds me the qbasic (feeling olddddd 😉 )

I wasn’t born when qBasic appeared haha! Will try both, and keep the more maintainable for the future

Create a function and pass the array. If something happens in the loop call the function again with the main array. Try this —

function check_loop($array) < foreach($array as $val) < if (something) check_loop($array); >> check_loop($array); 

Will try this solution, seems more understandable (I’m not alone to work on this project), and efficient. Will give you a feedback

You can use current(), next() and prev() to loop through array and move the internal array pointer back and forth:

$items = array("apple", "box", "cat"); while($item=current($items)) < print_r($item); if (needToGoBack($item)) // Go to previous array item $item = reset($items); >else < // Continue to next $item = next($items); >> 

But with next&prev, I can only go back to the precedent item ? Or maybe using them in another loop to do prev/prev/prev etc., but not sure it’s the faster solution :/

@HugoTor: Next will iterate through the array and if the condition to move back is satisfied, the prev() will move to previous item. I thought you wanted to go back to previous array item for some condition?

Источник

Оцените статью