-1

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.

3
  • 2
    Initialize an empty totalcount at the start of your foreach: $data[$row->id]['totalcount'] = 0; - also, you can just add $row->total to the totalcount, saved some complexity and characters. Commented Jun 16, 2016 at 2:25
  • even if using $data[$row->id]['totalcount'] += $row->total, still getting the notice. Commented Jun 16, 2016 at 2:26
  • From where the $data comes??? Commented Jun 16, 2016 at 2:32

2 Answers 2

2

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'];
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your suggestion, but if I add "$data[$row->id]['totalcount'] = 0;" right after "if ($id != $row->id)" then the count is wrong. totalcount becomes 2 and 0, and it should be 100 and 0
@Alko Make sure the initialization happens inside the if block. Also remember my note above: this setup will only work properly if the row IDs are sequential. If you jump back-and-forth between row ID 1 and row ID 2, it won't work because whenever the ID changes, the totalcount for that ID will also be reset. If you can't guarantee that the row IDs are sequential, you'll need to just do a simple isset check instead.
The first time I used isset, did not work, but for some reason it works now. Must have been typo on my end.Thank you very much.
0

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

1 Comment

Still not good I end up now with 22 and 0 instead of 100 and 0 as totalcount. You have reversed ? and : in your example.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.