0

Not sure why I'm getting this error as the code is working and I can see the data I expected.

Code

$allSales = array();
foreach ($game['sales'] as $sale) {
  foreach ($sale['values'] as $id => $values) {
    $allSales[$id]+=$values['y'];
  }
}  

Error 1

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 0

Filename: player/game.php

Line Number: 81

Error 2 (Same Code)

A PHP Error was encountered

Severity: Notice

Message: Undefined offset: 1

Filename: player/game.php

Line Number: 81
dvoutt
  • 930
  • 2
  • 9
  • 23
  • Thanks for the link but why is everyone on this site so ready to penalize people for asking a question that's been asked before. It's not like it's always easy to find the answer you are looking for. – dvoutt Aug 08 '14 at 22:16
  • We're not penalizing you, just trying to reduce redundancy. – Barmar Aug 08 '14 at 22:21
  • Don't you get negative points for a duplicate. Anyways I don't see the same issue on the list or I'm missing it. – dvoutt Aug 08 '14 at 22:23
  • No, you don't lose reputation when your question is closed. See http://meta.stackexchange.com/a/7238/189777 for the list of what affects your reputation. – Barmar Aug 08 '14 at 22:27
  • @dvoutt We are not trying to penalize you. "Undefined offset" questions have been asked and answered hundreds, if not thousands, of times before. By closing questions like this we are trying to reduce clutter, and consolidate good answers, making it easier for people to find helpful answers. – Sverri M. Olsen Aug 08 '14 at 23:18
  • Which is ironic since I couldn't find the good answer I just for and decided to ask. Just saying. – dvoutt Aug 08 '14 at 23:19
  • @dvoutt Are you looking for an answer to *this exact problem*, or you trying to *understand what the error message means*? If you are looking for the former then you are doing it wrong. If you are looking for the latter then you are not looking very well; there are hundreds of answers that explain it very well. – Sverri M. Olsen Aug 08 '14 at 23:27
  • I wanted and answer to that exact problem. So please enlighten me. – dvoutt Aug 08 '14 at 23:32
  • @SverriM.Olsen To be fair, the situation when using update operators like `+=` is not easily abstracted from the common causes of this error. Most people think of this as an assignment operator, and the fact that it's also a reader is often forgotten. – Barmar Aug 09 '14 at 04:38

1 Answers1

3

The statement:

$allSales[$id] += $values['y'];

means to take the current value of $allSales[$id], add the value of $values['y'] to it, and store that result back in $allSales[$id]. But the first time that you encounter a particular $id, there is no $allSales[$id]. This results in the warning when you try to get its current value.

Change to:

if (isset($allSales[$id])) {
    $allSales[$id] += $values['y'];
} else {
    $allSales[$id] = $values['y'];
}

Or you could just prefix the line with @ to suppress warnings (I know purists will cry over this):

@$allSales[$id] += $values['y'];
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks this worked perfectly. Sorry took me so long to mark it right, didn't get a chance till now to use it. – dvoutt Aug 09 '14 at 15:55