简体   繁体   English

如何增加多维数组的值

[英]How to increment a multi-dimensional array value

I am trying to increment the value of caps once it code sees that there is an event type of 10 or 11 in the loop. 一旦代码看到循环中的事件类型为10或11,我便尝试增加caps的值。 I am currently receiving an offset 1 error when I run the code, with the error being on the $red_stats[$i]['caps'] = $red_stats[$i]['caps']+1 line. 我现在在运行代码时收到偏移1错误,该错误在$red_stats[$i]['caps'] = $red_stats[$i]['caps']+1行上。

I am trying to achieve an end array structure like the following: 我正在尝试实现如下的结束数组结构:

$red_stats[$round]['caps']

I have the following code (everything else checks out except for the previously mentioned line.. as an added note, $round_count will evaluate to 2 so $i should evaluate to either 1 or 2 : 我有以下代码(除了前面提到的行,其他所有内容都检出了..作为补充, $round_count计算结果为2,因此$i计算结果为12

    $red_stats = [['caps'=>0]];
    array_pop($red_stats);

    for ($i = 1; $i <= $round_count; $i++)
    {
        foreach($events as $event)
        {
            if($event->event_type_id == 10 | $event->event_type_id == 11)
            {
                $red_stats[$i]['caps'] = $red_stats[$i]['caps'] + 1;
            }
        }
    }
    dd($red_stats);

You may need to do this inside of the if ($event...) brackets: 您可能需要在if($ event ...)括号内执行此操作:

if (isset($red_stats[$i]['caps']) && array_key_exists('caps', $red_stats[$i]))
{
  $red_stats[$i]['caps']++;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM