Welcome Guest, Not a member yet? Register   Sign In
autoincrement array values results in Undefined index
#1

[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.
#2

[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




Theme © iAndrew 2016 - Forum software by © MyBB