简体   繁体   English

php,foreach循环中的求和值

[英]Php, sum values in foreach loop

I cant get rid of "Notice: Undefined index: totalcount" in following example: 在以下示例中,我无法摆脱“通知:未定义的索引:totalcount”:

stdClass Object
(
    [1] => stdClass Object
        (
            [name] => How do you find our site?
            [id] => 1
            [values] => stdClass Object
                (
                    [0] => stdClass Object
                        (
                            [value] => Piece of cake
                            [count] => 10
                        )

                    [3] => stdClass Object
                        (
                            [value] => Very Easy
                            [count] => 20
                        )

                    [4] => stdClass Object
                        (
                            [value] => Easy
                            [count] => 30
                        )

                    [6] => stdClass Object
                        (
                            [value] => Hard
                            [count] => 40
                        )

                )

            [totalcount] => 100
        )

    [2] => stdClass Object
        (
            [name] => What is your favourite color?
            [id] => 2
            [values] => stdClass Object
                (
                    [1] => stdClass Object
                        (
                            [value] =>Green
                            [total] => 0
                        )

                    [2] => stdClass Object
                        (
                            [value] => Blue
                            [total] => 0
                        )

                    [5] => stdClass Object
                        (
                            [value] => Red
                            [total] => 0
                        )

                    [7] => stdClass Object
                        (
                            [value] => Black
                            [total] => 0
                        )

                )

            [totalcount] => 0
        )
)

Then I loop array using foreach to sort based on name value: 然后,我使用foreach循环数组以根据名称值进行排序:

  foreach ($array as $i => $row) {
      if ($id != $row->id) {
          $id = $row->id;
          $data[$row->id]['name'] = $row->question;
          $data[$row->id]['id'] = $row->id;
      }
      $data[$row->id]['opts'][$i]['value'] = $row->value;
      $data[$row->id]['opts'][$i]['count'] = $row->total;
      $data[$row->id]['totalcount'] += $data[$row->id]['opts'][$i]['count'];
  }

but keep getting index notice here " $data[$row->id]['totalcount'] += $data[$row->id]['opts'] " I'm not sure how to fix the problem. 但请继续在此处获取索引通知“” $data[$row->id]['totalcount'] += $data[$row->id]['opts'] “我不确定如何解决此问题。 The results are all correct, just the issue with that particular line. 结果都是正确的,只是该特定行的问题。

I just need to sum all values of " count " and assign as single value to totalcount 我只需要对“ 计数 ”的所有值求和并将其作为单个值分配给totalcount

Any help is appreciated. 任何帮助表示赞赏。

The notice is due to the fact that $data[$row->id]['totalcount'] is uninitialized before you try to add to it. 该通知的原因是,在尝试添加$ data [$ row-> id] ['totalcount']之前,该数据尚未初始化。 The notice doesn't affect the outcome in this situation, but PHP wants you to know what is going on. 该通知在这种情况下不会影响结果,但是PHP希望您知道正在发生什么。

The easiest way to remove the notice is to add an initialization line for that variable when you switch the current ID, but you can only do that if you are sure that you won't switch from row #1 to row #2 and back to row #1 again (or something similar). 删除通知的最简单方法是在切换当前ID时为该变量添加一个初始化行,但是只有在确保不会从第1行切换到第2行并返回到第2行的情况下,您才能这样做。再排第1行(或类似的内容)。 To do so, add 为此,添加

$data[$row->id]['totalcount'] = 0;

to your 给你

if ($id != $row->id)

block. 块。

The most foolproof solution is just to add the initialization line in combination with isset() . 最简单的解决方案是将初始化行与isset()结合使用。

Example: 例:

if (isset($data[$row->id]['totalcount']))
{
    $data[$row->id]['totalcount'] += $data[$row->id]['opts'][$i]['count'];
}
else
{
    $data[$row->id]['totalcount'] = $data[$row->id]['opts'][$i]['count'];
}

instead of 代替

$data[$row->id]['totalcount'] += $data[$row->id]['opts'][$i]['count'];

I would do it like this 我会这样

$data[$row->id]['totalcount'] = !isset($data[$row->id]['totalcount']):0?$data[$row->id]['totalcount'] + $data[$row->id]['opts'][$i]['count'];

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

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