CodeIgniter Forums
autoincrement array values results in Undefined index - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: autoincrement array values results in Undefined index (/showthread.php?tid=12150)



autoincrement array values results in Undefined index - El Forum - 10-08-2008

[eluser]K.OS[/eluser]
Hi,

i have a tiny problem with an array, this array will cotain database entries by date separated in a negative and positive counter:
Code:
foreach ($votes as $key => $value) {
            $thisDay = date('y-m-j',strtotime($votes[$key]->timeEnter));
            if($value->voting == 1) {
                $voteCount[$thisDay]['pos']++;
            }else{
                $voteCount[$thisDay]['neg']++;    
            }

        }
Whenever $thisDay changes to a new date, i get an error:
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined index: pos

i know that everytime i try to increment the value for a new date, the key for the array does not exist, which actually causes the problem, but i have no idea how to solve this.


autoincrement array values results in Undefined index - El Forum - 10-08-2008

[eluser]xwero[/eluser]
What about
Code:
if($value->voting == 1) {
   if( ! isset($voteCount[$thisDay]['pos'])
   {
      $voteCount[$thisDay]['pos'] = $value->voting;
   }
   else
   {
      $voteCount[$thisDay]['pos']++;
   }
} // rest of the code