douran7929 2010-05-29 04:58 采纳率: 100%
浏览 11
已采纳

PHP数组指针疯狂

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!

  • 写回答

1条回答 默认 最新

  • dqcz57269 2010-05-29 05:01
    关注

    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()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。