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.
totalcountat the start of your foreach:$data[$row->id]['totalcount'] = 0;- also, you can just add$row->totalto thetotalcount, saved some complexity and characters.$datacomes???