php foreach next iteration
How can I repeat a specific iteration in a foreach loop in PHP?
As there is no iterator in PHP, the only way to loop through an array without getting the length of the array is to use foreach loop.
Let say I have the following loop:
One way to do that is to use for loop instead.
3 Answers 3
Perhaps a do-while will work for you.
Or a single loop structure that destroys the input array as it iterates.
My final suggestion:
You are trying to handle two different things in your loop, that makes it hard to write clean control flow. You could separate the retry-logic from the result handling:
(there might be syntax errors, it’s been a while since I wrote PHP code)
To repeat a single specific iteration you need to add a control mechanism, it is not an intended behavior after all.
There are many suggestions here, all of them are kinda over-engineered.
PHP is a high level derivate of C and both languages have the ‘goto’ operator, it has a bad reputation because people have historically used it too much. In the end a foreach/while loop is internally nothing else than a ‘goto’ operation.
That’s how this should be done just keep in mind that this can cause an endless loop, like in the example.
Look at the next complicated version without goto:
This is essentially the same as the goto, just with more code. If you’d want to «break» or «continue» the foreach loop you’d write «break 2» or «continue 2»
Php foreach next iteration
You can also use the alternative syntax for the foreach cycle:
I cannot stress this point of the documentation enough! Here is a simple example of exactly why this must be done:
Even though it is not mentioned in this article, you can use «break» control structure to exit from the «foreach» loop.
WARNING: Looping through «values by reference» for «extra performance» is an old myth. It’s actually WORSE!
?>
Which do you think is faster?
Lots of people think the answer is two() because it uses «reference to value, which it doesn’t have to copy each value when it loops».
Well, that’s totally wrong!
Here’s what actually happens:
Alright, so what’s the second version doing? The beloved «iterate values by reference»?
— This function takes an array as argument ($arr).
— The array function argument itself isn’t passed by reference, so the function knows it isn’t allowed to modify the original at all.
— Then the foreach loop happens. The array itself wasn’t passed by reference to the function, so PHP knows that it isn’t allowed to modify the outside array.
— But it also sees that you want to look at all VALUES by reference (&$val), so PHP says «Uh oh, this is dangerous. If we just give them references to the original array’s values, and they assign some new value to their reference, they would destroy the original array which they aren’t allowed to touch!».
— So PHP makes a FULL COPY of the ENTIRE array and ALL VALUES before it starts iterating. YIKES!
Therefore: STOP using the old, mythological «&$val» iteration method! It’s almost always BAD! With worse performance, and risks of bugs and quirks as is demonstrated in the manual.
You can always manually write array assignments explicitly, without references, like this:
Get next element in foreach loop
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.
9 Answers 9
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 current and next functions, you could do this:
You could probably use while loop instead of foreach:
Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don’t rely on the array pointer during or after the foreach without resetting it.
If the indexes are continuous:
You could get the keys/values and index
if its numerically indexed:
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:
You could get the keys of the array before the foreach, then use a counter to check the next element, something like:
A foreach loop in php will iterate over a copy of the original array, making next() and prev() functions useless. If you have an associative array and need to fetch the next item, you could iterate over the array keys instead:
Since the resulting array of keys has a continuous index itself, you can use that instead to access the original array.
Using method 2 the complete foreach could look like this:
О тонкостях работы foreach в PHP
В недавнем дайджесте интересных ссылок о PHP я обнаружил ссылку на комментарий Никиты Попова на StackOverflow, где он подробно рассказывает о механизме «под капотом» управляющей конструкции foreach.
Поскольку foreach действительно иногда работает более, чем странным образом, я счел полезным сделать перевод этого ответа.
Внимание: этот текст подразумевает наличие базовых знаний о функциональности zval’ов в PHP, в частности вы должны знать что такое refcount и is_ref.
foreach работает с сущностями разных типов: с массивами, с простыми объектами (где перечисляются доступные свойства) и с Traversable-объектами (вернее, объектами, у которых определен внутренний обработчик get_iterator). Здесь мы, в основном, говорим о массивах, но я скажу и об остальных в самом конце.
Прежде чем приступить, пара слов о массивах и их обходе, важная для понимания контекста.
Как работает обход массивов
Массивы в PHP являются упорядоченными хеш-таблицами (элементы хеша объединены в двусвязный список) и foreach обходит массив, следуя указанному порядку.
Таким образом, внешние указатели массива могут быть использованы только когда вы полностью уверены, что при обходе никакого пользовательского кода выполняться не будет. А такой код может оказаться в самом неожиданном месте, типа обработчика ошибок или деструктора. Вот почему в большинстве случаев PHP приходится использовать внутренний указатель вместо внешнего. Если бы это было иначе, PHP мог бы упасть из-за segmentation fault, как только пользователь начнет делать что-нибудь необычное.
Проблема внутреннего указателя в том, что он является частью HashTable. Так что, когда вы изменяете его, HashTable меняется вместе с ним. И коль скоро обращение к массивам в PHP делается по значению (а не по ссылке), вы вынуждены копировать массив, чтобы в цикле обходить его элементы.
Простой пример, показывающий важность копирования (кстати, не такая большая редкость), это вложенная итерация:
Здесь вы хотите чтобы оба цикла были независимым, а не хитро перебрасывались одним указателем.
Итак, мы дошли до foreach.
Обход массива в foreach
Теперь вы знаете, для чего foreach приходится создавать копию массива, прежде чем обойти его. Но это явно не вся история. Сделает PHP копию или нет, зависит от нескольких факторов:
Итак, это первая часть тайны: функция копирования. Вторая часть это то, как текущая итерация выполняется, и она тоже довольно странная. «Обычный» образец итерации, который вы уже знаете (и который часто используется в PHP — отдельно от foreach) выглядит примерно так (псевдокод):
итерация foreach выглядит немного иначе:
Такой режим работы foreach также является причиной, по которой внутренний указатель массива переходит к следующему элементу, если текущий удалён, а не к предыдущему (как вы могли бы ожидать). Всё сделано так, чтобы отлично работать с foreach (но, очевидно, со всем остальным будет работать не так хорошо, пропуская элементы).
Последствия для кода
Первое следствие вышеописанного поведения в том, что foreach копирует итерируемый массив в многих случаях (медленно). Но отриньте страх: я пробовал удалить требование копирования и не смог увидеть ускорения работы нигде, кроме искусственных бенчмарков (в которых итерация происходила в два раза быстрее). Похоже, люди просто не итерируют достаточно много.
Второе следствие в том, что обычно не должно быть других следствий. Поведение foreach, в основном, вполне понятно пользователю и просто работает как следует. Вас не должно волновать, как происходит копирование (и происходит ли оно вообще), и в какой конкретно момент времени перемещается указатель.
И третье следствие — и тут мы как раз подходим к вашим проблемам — в том, что иногда мы видим очень странное поведение, которое трудно понять. Это происходит конкретно тогда, когда вы пытаетесь модифицировать сам массив, который вы обходите в цикле.
Большую коллекцию поведения в пограничных случаях, которые появляются, когда вы модифицируете массив в ходе итерации, можно найти в тестах PHP. Вы можете начать с этого теста, после чего изменять 012 на 013 в адресе, и так далее. Вы увидите, как поведение foreach будет проявляться в разных ситуациях (всякие комбинации ссылок и.т.д.).
А сейчас вернёмся к вашим примерам:
Та же ситуация, что и в первом тесте.
Но эти примеры недостаточно убедительны. Поведение начинает быть по настоящему непредсказуемым, когда вы используете current в цикле:
Теперь попробуем сделать небольшое изменение:
Здесь у нас is_ref=1, так что массив не копирован (так как и выше). Но сейчас когда есть is_ref, массив больше не нужно разделять, передавая по ссылке к current. Теперь current и foreach работают с одним массивом. Вы видите массив сдвинутым на единицу как раз из-за того, как foreach обращается с указателем.
То же самое вы увидите, когда будете делать обход массива по ссылкам:
Еще одна небольшая вариация, здесь мы присвоим наш массив еще одной переменной:
Итерация объектов
При итерации объектов имеет смысл рассмотреть два случая:
Объект не Traversable (вернее, не определен внутренний обработчик get_iterator)
В этом случае итерация происходит почти так же, как у массивов. Та же семантика копирования. Единственное отличие: foreach запустит некий дополнительный код, чтобы пропустить свойства, недоступные в текущей области видимости. Еще пара интересных фактов:
Объект Traversable
В этом случае всё, что сказано выше, не будет применяться никоим образом. Также PHP не будет копировать и не будет применять никакие трюки вроде увеличения указателя до прохода цикла. Я думаю что режим прохода по обходимому (Traversable) объекту куда более предсказуем и не требует дальнейшего описания.
Замена итерируемого объекта во время цикла
Другой необычный случай, который я не упомянул — PHP допускает возможность замены итерируемого объекта во время цикла. Вы можете начать с одним массивом и продолжить, заменив его на полдороге другим. Или начать с массивом, в затем заменить его объектом:
Как видите, PHP просто начал обходить другую сущность, как только произошла замена.
Изменение внутреннего указателя массива во время итерации
Последняя деталь поведения foreach, которую я не упомянул (потому что может быть использована для получения по настоящему странного поведения): что может случиться если попытаться изменить внутренний указатель массива во время прохода цикла.
Тут вы можете получить не то, что ожидали: если вызывать next или prev в теле цикла (в случае передачи по ссылке), вы увидите, что внутренний указатель переместился, но это никак не повлияло на поведение итератора. Причина в том, что foreach делает бекап текущей позиции и хеша текущего элемента в HashPointer после каждого прохода цикла. На следующей проходе foreach проверит, не менялась ли позиция внутреннего указателя и попытается восстановить ее, используя этот хеш.
Давайте посмотрим что означает «попытается». Первый пример показывает, как изменение внутреннего указателя не меняет режим foreach:
Теперь давайте попробуем сделать unset элементу, к которому обратится foreach при первом проходе (ключ 1):
Тут вы увидите, что счетчик сброшен, так как не удалось найти элемент с подходящим хешом.
Имейте в виду, хеш — всего лишь хеш. Случаются коллизии. Попробуем теперь так:
Работает так, как мы и ожидали. Мы удалили ключ EzFY (тот, где как раз был foreach), так что был сделан сброс. Также мы добавили дополнительный ключ, поэтому в конце мы видим 4.
И вот тут приходит неведомое. Что произойдёт, если заменить ключ FYFY с FYFZ? Давайте попробуем:
Сейчас цикл перешёл непосредственно к новому элементу, пропуская всё остальное. Это потому что ключ FYFY имеет коллизию с EzFY (вообще-то, все ключи из этого массива тоже). Более этого, элемент FYFY находится по тому же адресу в памяти, что и элемент EzFY который только что был удален. Так что для PHP это будет та же самая позиция с тем же хешом. Позиция «восстановлена» и происходит переход к концу массива.
Php foreach next iteration
(PHP 4, PHP 5, PHP 7, PHP 8)
next — Перемещает указатель массива вперёд на один элемент
Описание
Список параметров
Массив ( array ), изменяемый данной функцией.
Возвращаемые значения
Примеры
Пример #1 Пример использования next() и связанных функций
Примечания
Смотрите также
User Contributed Notes 16 notes
Now from PHP 7.2, the function «each» is deprecated, so the has_next I’ve posted is no longer a good idea. There is another to keep it simple and fast:
Don’t confuse next with continue!
If you’re a Perl developer starting with PHP, you might try to use «next» inside a loop to skip to the next iteration.
The php compiler will take next. but it’s not going to work.
Papipo’s function below is usefull in concept but does not work.
«Since you do not pass the array by reference, its pointer is only moved inside the function.»
This is true, but the array you are manipulating in your has_next() function will have it’s pointer set to the first element, not the same position as the original array. What you want to do is pass the array to the has_next() function via reference. While in the has_next() function, make a copy of the array to work on. Find out the current pointer position of the original array and set the pointer on the working copy of the array to the same element. Then you may test to see if the array has a «next» element.
Try the followig insetad:
This code returns neighbors of the specified key. The result will be empty if it doesn’t have any neighbors. My approach was to use the order of keys to determine neighbors, which is differnet from just getting the next/previous element in an array. Feel free to point out stupidities 🙂
I need to know if an array has more items, but without moving array’s internail pointer. Thats is, a has_next() function:
// prints ‘melon’
?>
Since you do not pass the array by reference, its pointer is only moved inside the function.
Hope that helps.
regarding references with foreach, you can use them directly. Obviating various posts which provide many lines of ‘work arounds’.
foreach($array as &$value)
Take care when replacing code using reset()/next() with code using foreach as foreach does not update the array’s internal pointer. This means you cannot, say, use next() to skip an element in foreach loop, or use current() within a function to get a reference to the current element. You probably have code depending on this internal pointer and replacing it will be more work than you anticipated.
This function returns next element in array after your key or false if it last or key doesn’t exists in array.
a more readable version of papipo’s has_next function:
brentimus’ array_set_pointer function will only work if the array value is unique in the array, and none of the array values are FALSE. It would be more reliable to use key() instead of current(). For similar reasons it’s better to check key() after calling next() to determine whether the next() element «exists». Simply checking the value returned by next() will produce a false negative when looking at, for example, the first element of the array: [‘one’, 0, ‘three’]
However, it also turns out that the copied array retains the original array’s pointer, so array_set_pointer is not actually required here. The following should work:
This class implements simple operations with array
public function __construct () <
public function getCurrent () <
public function getNext () <
this may be handy and i didnt know where else to post it.. i need a simple function to cycle through an array i eventually made it into a class so i could have multiple cycles.. if you like it or find it usefull please email me and let me know
function Cycle()
<
$this->dataArray = func_get_args();
$this->dataArrayCount = count($this->dataArray);
>
$bgColor = new Cycle(‘#000000’, ‘#FFFFFF’, ‘#FF0000’);