为了账号安全,请及时绑定邮箱和手机立即绑定

PHP:如果没有赋值,为什么要修改值?

PHP:如果没有赋值,为什么要修改值?

PHP
慕莱坞森 2021-09-18 10:53:24
基本上没有太多关于它的内容,因为我放入了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:这是 vars 的声明方式:$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
查看完整描述

1 回答

?
富国沪深

TA贡献1790条经验 获得超9个赞

我发现了问题。在脚本的主循环中,我也有这个东西:


if($currentTime - $settings->lastHarvestIncrease > 3)

{

    foreach($mapHarvests as &$section)

        if($section[$timeToHarvest] > 1)

            $section[$timeToHarvest] --;


    $settings->lastHarvestIncrease = $currentTime;

    $settings->save();

}

似乎更改section为section2给出了正确的结果。$section仅用于这两个部分,它们位于对角,范围应该有所不同,但我想我不明白引用是如何工作的。




查看完整回答
反对 回复 2021-09-18
  • 1 回答
  • 0 关注
  • 153 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信