Hướng dẫn foreach php next element
I have a foreach loop and I want to see if there is a next element in the loop so I can compare the current element with the next. How can I do this? I've read about the current and next functions but I can't figure out how to use them. Show Nội dung chính
Thanks in advance asked Feb 23, 2011 at 20:38 1 A unique approach would be to reverse the array and then loop. This will work for non-numerically indexed arrays as well:
If you are still interested in using the
#2 is probably the best solution. Note, answered Feb 23, 2011 at 20:43 StephenStephen 18.6k9 gold badges59 silver badges98 bronze badges 0 You could probably use while loop instead of foreach:
Lucas 16.5k30 gold badges105 silver badges173 bronze badges answered Oct 21, 2011 at 13:16 pronskiypronskiy 1,3391 gold badge11 silver badges8 bronze badges 1 As php.net/foreach points out:
In other words - it's not a very good idea to do what you're asking to do. Perhaps it would be a good idea to talk with someone about why you're trying to do this, see if there's a better solution? Feel free to ask us in ##PHP on irc.freenode.net if you don't have any other resources available. answered Feb 24, 2011 at 8:05 TMLTML 12.5k3 gold badges36 silver badges44 bronze badges If the indexes are continuous:
Tim 2,4461 gold badge23 silver badges30 bronze badges answered Feb 23, 2011 at 20:42 Mārtiņš BriedisMārtiņš Briedis 17.1k5 gold badges53 silver badges74 bronze badges 2 You could get the keys/values and index
result:
answered Mar 23, 2018 at 15:17 Andrei KrasutskiAndrei Krasutski 4,5051 gold badge25 silver badges34 bronze badges 1 if its numerically indexed:
Rob 399k70 gold badges751 silver badges975 bronze badges answered Feb 23, 2011 at 20:43 The general solution could be a caching iterator. A properly implemented caching iterator works with any Iterator, and saves memory. PHP SPL has a CachingIterator, but it is very odd, and has very limited functionality. However, you can write your own lookahead iterator like this:
answered Mar 14, 2016 at 19:50 Dávid HorváthDávid Horváth 3,8741 gold badge21 silver badges33 bronze badges You could get the keys of the array before the foreach, then use a counter to check the next element, something like:
This will work for both simple arrays, such as
answered Feb 23, 2011 at 21:10 eclipse31eclipse31 3644 silver badges15 bronze badges 1 A foreach loop in php will iterate over a copy of the original array, making
Since the resulting array of keys has a continuous index itself, you can use that instead to access the original array. Be aware that
Using method 2 the complete foreach could look like this:
answered Feb 14, 2016 at 16:28 LiquinautLiquinaut 3,5911 gold badge20 silver badges17 bronze badges 1
answered Feb 13 at 15:58 SirCumzSirCumz 1611 gold badge2 silver badges14 bronze badges How to get next array element in PHP?The next() function moves the internal pointer to, and outputs, the next element in the array. Related methods: prev() - moves the internal pointer to, and outputs, the previous element in the array. current() - returns the value of the current element in an array. 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. How to go to next iteration in for loop PHP?continue ¶ continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration. Note: In PHP the switch statement is considered a looping structure for the purposes of continue . How do you find the next value in an array?defineProperty(Array. prototype, "next", { value: function() { return this[++this.. By making a standalone function that accepts the array and returns the iterator, or.. By subclassing Array and overriding the iterator in the subclass, or.. |