PHP 7 和 PHP5 foreach 迭代指针移动区别

<?php
class a 
{
    private static  $arrCfg = array(
		300  =>array('money'=>1000,'tlen' =>300),	
		900  =>array('money'=>1200,'tlen' =>900),
		1800 =>array('money'=>1500,'tlen' =>1800),
		3000 =>array('money'=>2500,'tlen' =>3000),
	);
	public function getNext($tlen = 0)
	{
		if(!isset(self::$arrCfg[$tlen])){
			return array();
		}
		foreach (self::$arrCfg as $key=>$val)
		{
			if($key == $tlen)
			{
				$next = next(self::$arrCfg);
				return $next;
			}
			next(self::$arrCfg);
		}
	}
}
$a = new a();
print_r($a->getNext(300));
?>

PHP 7 下运行:

Array
(
    [money] => 1200
    [tlen] => 900
)

PHP 5 运行:

Array
(
    [money] => 1500
    [tlen] => 1800
)

<?php
class a 
{
    private static  $arrCfg = array(
		300  =>array('money'=>1000,'tlen' =>300),	
		900  =>array('money'=>1200,'tlen' =>900),
		1800 =>array('money'=>1500,'tlen' =>1800),
		3000 =>array('money'=>2500,'tlen' =>3000),
	);
	public function getNext($tlen = 0)
	{
		if(!isset(self::$arrCfg[$tlen])){
			return array();
		}
		foreach (self::$arrCfg as $key=>$val)
		{
			if($key == $tlen)
			{
				$next = current(self::$arrCfg);
				return $next;
			}
// 			next(self::$arrCfg);
		}
	}
}
$a = new a();
print_r($a->getNext(300));
?>

Array
(
    [money] => 1000
    [tlen] => 300
)

PHP 5 运行:

Array
(
    [money] => 1200
    [tlen] => 900
)



目测是 PHP5 foreach 循环数组指针下移一位,PHP7 则不移动。

展开阅读全文

没有更多推荐了,返回首页