Skip to content

Instantly share code, notes, and snippets.

@cirpo
Created August 20, 2010 01:21
Show Gist options
  • Select an option

  • Save cirpo/539379 to your computer and use it in GitHub Desktop.

Select an option

Save cirpo/539379 to your computer and use it in GitHub Desktop.
array pointer
<?php
class ArrayTest extends PHPUnit_Framework_TestCase {
public function testNextAndPrev1(){
$rs = array(1=>1,2=>2,3=>3,4=>4,5=>5);
$curID = 3;
reset($rs);
$prev = null;
foreach ($rs as $ID => $r){
if ($ID==$curID) break;
$prev=$r;
}
$next = current($rs);
$this->assertEquals($prev,2);
$this->assertEquals($next,4);
}
public function testNextAndPrev2(){
$rs = array(1=>1,2=>2,3=>3,4=>4,5=>5);
$curID = 3;
list($prev, $next) = $this->getBackNext2($rs, $curID);
$this->assertEquals($prev,2);
$this->assertEquals($next,4);
}
public function testNextAndPrev3(){
$rs = array(1=>1,2=>2,3=>3,4=>4,5=>5);
$curID = 3;
list($prev, $next) = $this->getBackNext3($rs, $curID);
$this->assertEquals($prev,2);
$this->assertEquals($next,4);
}
private function getBackNext2($rs, $curID=null){
reset($rs);
$prev = null;
foreach ($rs as $ID => $r) {
if ($ID==$curID) break;
$prev=$r;
}
$next = current($rs);
return array($prev, $next);
}
private function getBackNext3(&$rs, $curID=null){
/*non necessario in quanto l'array e' referenziato e il pointer voluto potrebbe essere in un'altra posizione, anche se non e' questo il caso*/
reset($rs);
$prev = null;
foreach ($rs as $ID => $r) {
if ($ID==$curID) break;
$prev=$r;
}
$next = current($rs);
return array($prev, $next);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment