[SOLVED] PHP each, next, prev, etc...

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Feb 17, 2005
  • Posts: 1585
  • Status: Offline

Post November 4th, 2009, 6:51 am

I'm trying to re-program a class that someone wrote that has to work with legacy drivers that use while() loops to iterate. I can't seem to get the last element to return any data after it temporarily goes off of the end of the array.

I ended up making an iterator function that looks like this:

PHP Code: [ Select ]
<?php
public function iterate()
{
 if(each($this->data['fruits']) !== FALSE)
 {
 return TRUE;
 }
 else
 {
 reset($this->data['fruits']);
 return FALSE;
 }
}
?>
  1. <?php
  2. public function iterate()
  3. {
  4.  if(each($this->data['fruits']) !== FALSE)
  5.  {
  6.  return TRUE;
  7.  }
  8.  else
  9.  {
  10.  reset($this->data['fruits']);
  11.  return FALSE;
  12.  }
  13. }
  14. ?>


Then to gather the current element so I can use the data in any function within the loop, I wrote a function like this:
PHP Code: [ Select ]
<?php
public function current_fruit()
{
$fruit_array = prev($this->data['fruits']);
next($this->data['fruits']);
return $fruit_array;
}
?>
  1. <?php
  2. public function current_fruit()
  3. {
  4. $fruit_array = prev($this->data['fruits']);
  5. next($this->data['fruits']);
  6. return $fruit_array;
  7. }
  8. ?>


And during each iteration current_fruit() can be called multiple times. It works, until the set of fruits, I don't get any data. Now the problem is, the loop properly makes enough iterations, but the last element has no data. I realize the each() function sends the pointer temporarily off the end of the array on the last iteration, but prev() should re-point it to the last element, right?
There's no place like 127.0.0.1, badass part is now it's ::1
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post November 4th, 2009, 6:51 am

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Feb 17, 2005
  • Posts: 1585
  • Status: Offline

Post November 4th, 2009, 8:50 am

I ended up using end() if prev() returned false. I guess if it goes off the array, you cannot use prev(), you have to reset the entire array to the end() element.
There's no place like 127.0.0.1, badass part is now it's ::1
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13458
  • Loc: Florida
  • Status: Offline

Post November 4th, 2009, 9:37 am

There's a joke about fruits going off the deep end in there somewhere. :D

Have you ever used the SPL Iterator classes ?
http://www.php.net/manual/en/class.arrayiterator.php
Strong with this one, the sudo is.
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Joined: Feb 17, 2005
  • Posts: 1585
  • Status: Offline

Post November 9th, 2009, 1:06 am

Aww crap I wish I had checked my e-mail, I could have used this. I was using multi-dimensional type array iteration and I ended up having to save it to a datatype, I wouldn't of had to with this.

Thanks man, this will be useful in the future.
There's no place like 127.0.0.1, badass part is now it's ::1

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 126 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.