Page 1 of 1

Getting Next item in array in a foreach loop

Posted: Sat Jan 14, 2006 12:27 pm
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'];
  }
  ?>

Posted: Sun Jan 15, 2006 8:15 pm
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];
}

Posted: Mon Jan 16, 2006 4:41 am
by Jenk

Posted: Mon Jan 16, 2006 9:18 am
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

Posted: Mon Jan 16, 2006 9:41 am
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
:?

Posted: Mon Jan 16, 2006 9:53 am
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

?>

Posted: Mon Jan 16, 2006 9:54 am
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)

Posted: Mon Jan 16, 2006 10:44 am
by jwalsh
Thanks guys, does exactly what I needed! I wasn't sure about using next() in a foreach, so this is very helpful.