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 fo开发者_开发技巧r the long post. Thanks for your help!
You should read the first two notes on the foreach documentation page:
Note: When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.
Note: Unless the array is referenced, foreach operates on a copy of the specified array and not the array itself. foreach has some side effects on the array pointer. Don't rely on the array pointer during or after the foreach without resetting it.
Making the array a reference will I believe fix your problem, but it's probably cleaner to just remember the last value in another variable instead of using prev()
精彩评论