It should work, maybe just add isset($items[$i+1]) or if($i < $itemcount - 1) before using it, because on last element it will crash..
php-finding next array value in a for-loop
Question
PHP question. Running the values of an array through a for loop, and need to find the next value, as a reference based on the value currently in the loop.
I created the array below:
$item_number: A12345-AFA-21-AEA-22-APA-23
$items = explode("-", $item_number);
i'm using a for loop that starts at AFA:
for ($i=1; $i<$itemcount; $i++)
setting the current loop value to $item.
$item = $items[$i];
How do I get the next value in the array? here is what seems the most logical to me.. but doesn't seem to work.
$next = $items[$i+1]
How do I get to the next array value?
Solution
OTHER TIPS
This should work, but keep in mind that an array starts at 0 not 1, so you need to change it to:
for ($i=0; $i<$itemcount; $i++){
$item = $items[$i];
$next = $items[$i+1];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow