PHP: 没有赋值为什么要修改值?
PHP: why the value is modified if there is no assign?
基本上没有太多关于它的,因为我把 echo
放在我的代码中。这是一个 CLI 脚本,应该是单线程的。
echo "$map: ".json_encode($map)."\n$mapHarvests: ".json_encode($mapHarvests)."\n";
foreach($map as $key => $section)
if($players[$id]->pos < $section[0])
break;
elseif($players[$id]->pos < $section[1] && isset($mapHarvests[$key]))
{
$harvesters[$id] = [$currentTime, $key];
break;
}
echo "$map: ".json_encode($map)."\n$mapHarvests: ".json_encode($mapHarvests)."\n";
这是控制台输出的内容:
$map: [[-560,-240],[240,560]]
$mapHarvests: [[[0],1],[[1,2,3],1]]
$map: [[-560,-240],[240,560]]
$mapHarvests: [[[0],1],[240,560]]
为什么要修改$mapHarvests
?我尝试将 isset()
与 array_key_exists()
切换,结果相同。有更牛逼的看代码:
foreach($map as $key => $section)
if(sectionStartsAfterPlayerPos())
break;
elseif(playerIsInSection() && sectionCanBeHarvested())
{
registerPlayer();
break;
}
编辑 1:变量是这样声明的:
$map = [0 => [-560, -240], 1 => [240, 560]];
$mapHarvests = [0 => [[0], 1], 1 => [[1, 2, 3], 1]];
$harvesters = [];
$currentTime = time(); // this one is inside the main loop
我发现了问题。在脚本的主循环中我也有这个东西:
if($currentTime - $settings->lastHarvestIncrease > 3)
{
foreach($mapHarvests as &$section)
if($section[$timeToHarvest] > 1)
$section[$timeToHarvest] --;
$settings->lastHarvestIncrease = $currentTime;
$settings->save();
}
似乎将 section
更改为 section2
给出了正确的结果。 $section
仅在这两个部分中使用,它们位于对角,范围应该不同,但我想我不明白引用是如何工作的。
基本上没有太多关于它的,因为我把 echo
放在我的代码中。这是一个 CLI 脚本,应该是单线程的。
echo "$map: ".json_encode($map)."\n$mapHarvests: ".json_encode($mapHarvests)."\n";
foreach($map as $key => $section)
if($players[$id]->pos < $section[0])
break;
elseif($players[$id]->pos < $section[1] && isset($mapHarvests[$key]))
{
$harvesters[$id] = [$currentTime, $key];
break;
}
echo "$map: ".json_encode($map)."\n$mapHarvests: ".json_encode($mapHarvests)."\n";
这是控制台输出的内容:
$map: [[-560,-240],[240,560]]
$mapHarvests: [[[0],1],[[1,2,3],1]]
$map: [[-560,-240],[240,560]]
$mapHarvests: [[[0],1],[240,560]]
为什么要修改$mapHarvests
?我尝试将 isset()
与 array_key_exists()
切换,结果相同。有更牛逼的看代码:
foreach($map as $key => $section)
if(sectionStartsAfterPlayerPos())
break;
elseif(playerIsInSection() && sectionCanBeHarvested())
{
registerPlayer();
break;
}
编辑 1:变量是这样声明的:
$map = [0 => [-560, -240], 1 => [240, 560]];
$mapHarvests = [0 => [[0], 1], 1 => [[1, 2, 3], 1]];
$harvesters = [];
$currentTime = time(); // this one is inside the main loop
我发现了问题。在脚本的主循环中我也有这个东西:
if($currentTime - $settings->lastHarvestIncrease > 3)
{
foreach($mapHarvests as &$section)
if($section[$timeToHarvest] > 1)
$section[$timeToHarvest] --;
$settings->lastHarvestIncrease = $currentTime;
$settings->save();
}
似乎将 section
更改为 section2
给出了正确的结果。 $section
仅在这两个部分中使用,它们位于对角,范围应该不同,但我想我不明白引用是如何工作的。