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

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

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?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

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.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

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
  • PolishHurricane
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1585

Post 3+ Months Ago

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.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 117 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
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.