Alko
Alko

Reputation: 1439

Php, sum values in foreach loop

I cant get rid of "Notice: Undefined index: totalcount" in following example:

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 ($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. 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

Any help is appreciated.

Upvotes: -1

Views: 3010

Answers (2)

massquote
massquote

Reputation: 4617

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

Upvotes: 0

badandyomega
badandyomega

Reputation: 252

The notice is due to the fact that $data[$row->id]['totalcount'] is uninitialized before you try to add to it. The notice doesn't affect the outcome in this situation, but PHP wants you to know what is going on.

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). 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().

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

Upvotes: 2

Related Questions