Php,foreach循环中的和值

时间:2016-06-16 02:21:11

标签: php foreach sum

我无法摆脱以下示例中的“注意:未定义索引: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
        )
)

然后我使用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'];
  }

但请继续在此处获取索引通知“$data[$row->id]['totalcount'] += $data[$row->id]['opts']” 我不知道如何解决这个问题。结果都是正确的,只是该特定行的问题。

我只需要将“计数”的所有值相加,并将其作为单个值分配给 totalcount

感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

该通知是由于$ data [$ row-> id] [' totalcount']在您尝试添加之前未初始化。在这种情况下,通知不会影响结果,但PHP希望您知道发生了什么。

删除通知的最简单方法是在切换当前ID时为该变量添加初始化行,但只有在确定不会从第1行切换到行时才能这样做#2再次返回第1行(或类似的东西)。为此,请添加

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

到你的

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

块。

最简单的解决方案是将初始化行与 isset() 结合使用。

示例:

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'];
}

答案 1 :(得分:0)

而不是

app.directive('googleAnalytics', function(configFactory){ 
  return {
    restrict: 'E',
    replace: true,
    link : function(scope,element,attrs){
      configFactory.getconfigs().then(function(configs) {
        scope.configs = configs;
      });
      scope.configs.forEach(function(config){
        console.log(config.ga_id);
      })
    },
    template: function(elem, attr){
      return "<script>(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,'script','https://www.google-analytics.com/analytics.js','ga');ga('create', '', 'auto');</script>"
    }
  }
})

我会这样做

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