Getting Next item in array in a foreach loop

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Getting Next item in array in a foreach loop

Post by jwalsh »

Hi,

I'm trying to generate a sort routine using a sortid in a database with up and down buttons to move items up and down in the list. This is fairly common, but I'm a little confused on how to do this.

I've gotten the up button to work fine by grabbing the previous item array, but how can I grab the next item in the array to get the id?

Thanks,

Code: Select all

<?
foreach ($DB->all_rows AS $product) {
  	$THISPRODUCT = new ProductX;
	$THISPRODUCT->id = $product['id'];
	$THISPRODUCT->GetById();
	  ?>
	  <tr>
		<td><? if ($UPONE) echo "[<a href='swap.php?id1=$UPONE&id2={$THISPRODUCT->id}'>up</a>]"; ?><br />
			[down]</td>
		<td><? echo $THISPRODUCT->title; ?></td>
		<td><a href="editproductx.php?id=<? echo $THISPRODUCT->id; ?>">Edit</a></td>
	  </tr>
	  <?
	 $UPONE = $product['id'];
  }
  ?>
ryanlwh
Forum Commoner
Posts: 84
Joined: Wed Sep 14, 2005 1:29 pm

Post by ryanlwh »

If the indices of the array are integers, then:

Code: Select all

foreach($array as $key=>$value)
{
   $prev = $array[$key-1];
   $next = $array[$key+1];
}
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Jenk wrote:next()
Be careful with this tho, becuase on the next itteration of the loop, you will miss out one of the array values. So basically you would only read every other value, unless you reset the internal array pointer back to where is sould be
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Pimptastic wrote:Be careful with this tho, becuase on the next itteration of the loop, you will miss out one of the array values. So basically you would only read every other value, unless you reset the internal array pointer back to where is sould be
PHP Manual Comment wrote: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
:?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

hehe, maybe im wrong...it has been known :lol: (although im sure i have had problems with next() in the past)

Just proven it to myself

Code: Select all

<?php

$numbers = array("1", "2", "3", "4");

foreach($numbers as $temp) {

	echo $temp."<br />";
	next($numbers);

}
// outputs
// 1
// 2
// 3
// 4

?>
Last edited by JayBird on Mon Jan 16, 2006 9:59 am, edited 4 times in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

this code:

Code: Select all

<?php
        $arr = range(1,10);
        foreach($arr as $i) {
                var_dump($i, next($arr));
                echo "\n";
        }
?>
produces the following output:

Code: Select all

int(1)
int(2)

int(2)
int(3)

int(3)
int(4)

int(4)
int(5)

int(5)
int(6)

int(6)
int(7)

int(7)
int(8)

int(8)
int(9)

int(9)
int(10)

int(10)
bool(false)
It means you can use foreach() with next() for the purposes outlined by OP. Yet you should be careful and execute next() on each iteration. Otherwise your loop will get out of sync with itself. Consider the following snippet:

Code: Select all

<?php
        $arr = range(1,10);
        foreach($arr as $i) {
                if( $i % 2 == 0)
                        next($arr);
                var_dump($i, current($arr));
                echo "\n";
        }
?>
Results:

Code: Select all

int(1)
int(1)

int(2)
int(2)

int(3)
int(2)

int(4)
int(3)

int(5)
int(3)

int(6)
int(4)

int(7)
int(4)

int(8)
int(5)

int(9)
int(5)

int(10)
int(6)
User avatar
jwalsh
Forum Contributor
Posts: 202
Joined: Sat Jan 03, 2004 4:55 pm
Location: Cleveland, OH

Post by jwalsh »

Thanks guys, does exactly what I needed! I wasn't sure about using next() in a foreach, so this is very helpful.
Post Reply