PHP array pointer craziness
- by JMan
I'm trying to create a "GetCurrentLevel" method that takes a point value as an input and returns what "Level" that corresponds to. I'm storing the Level = Points mapping in an array, but the array pointer is not moving logically when I use it a foreach loop. I've added echo statements for debugging. Here's my class definition:
class Levels extends Model
{
    protected $_map = array (
                           'None'   => 0,
                           'Bronze' => 50,
                           'Silver' => 200,
                           'Gold'   => 500
                           );
public function __construct()
{
        parent::__construct();
}
    public function GetCurrentLevel($points)
    {
        foreach ($this->_map as $name => $threshold)
         {
            echo "Level Name: $name<br/>";
            echo "Level Threshold: $threshold<br/>";
            echo "Current Level: " . key($this->_map) . "<br/>";
            echo "Current Threshold: " . current($this->_map) . "<br/>";
             if ($points < $threshold)       /* Threshold is now above the points, so need to go back one level */
             {
                 $previousThreshold = prev($this->_map);
                 echo "Previous Threshold: $previousThreshold<br/>";
                 echo "Final Level: " . key($this->_map) . "<br/>";
                 return key($this->_map);
             }
             echo "Go to next level <br/>";
         }
}
And here is what I see when I call GetCurrentLevel(60):
Level Name: None
Level Threshold: 0
Current Level: Bronze        //* Looks like foreach immediately moves the array pointer *//
Current Threshold: 50
Go to next level 
Level Name: Bronze
Level Threshold: 50
Current Level: Bronze        //* WTF? Why hasn't the array pointer moved? *//
Current Threshold: 50
Go to next level 
Level Name: Silver
Level Threshold: 200
Current Level: Bronze         //* WTF? Why hasn't the array pointer moved? *//
Current Threshold: 50
Previous Threshold: 0
Final Level: None
But the "Final Level" should be 'Bronze' since 60 points is above the 50 points needed for a Bronze medal, but below the 200 points needed for a Silver medal.
Sorry for the long post. Thanks for your help!