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

基本上没有太多关于它的内容,因为我放入了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


慕莱坞森
浏览 154回答 1
1回答

富国沪深

我发现了问题。在脚本的主循环中,我也有这个东西:if($currentTime - $settings->lastHarvestIncrease > 3){&nbsp; &nbsp; foreach($mapHarvests as &$section)&nbsp; &nbsp; &nbsp; &nbsp; if($section[$timeToHarvest] > 1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $section[$timeToHarvest] --;&nbsp; &nbsp; $settings->lastHarvestIncrease = $currentTime;&nbsp; &nbsp; $settings->save();}似乎更改section为section2给出了正确的结果。$section仅用于这两个部分,它们位于对角,范围应该有所不同,但我想我不明白引用是如何工作的。
打开App,查看更多内容
随时随地看视频慕课网APP