1

I am processing the result of a GROUP BY query which does not produce result for all the permutation of constants. It obviously gives an "Undefined Index" notice which I want to get of.

Of course I could use isset but it would mean duplication of code (of the index). An other way would be to use a variable to store the index, but it would mean every such line of code is now two line. I like the readability of this line of code.

$stat->setMapped($companyStats["Account"][mypackage_MyClass::$STATUS_ACCEPTED]);

Is there any way (except the error operator @ to avoid this notice)?

vbence
  • 20,084
  • 9
  • 69
  • 118

2 Answers2

1

You should start with an array that has all the keys defined and then merge your array into it. The result will have your values where they are defined and null (or whatever defaults you set) where they were not.

$allTheKeysYouWant = array('key1' => null, 'key2' => null, ...);
$keysFilled = array_merge($allTheKeysYouWant, $yourArray);
Okonomiyaki3000
  • 3,628
  • 23
  • 23
1

In the setMapped function declaration, accept the parameter by reference, i.e.

function setMapped(&$companyStats) {
    if (isset($companyStats)) {
         ....
snez
  • 2,400
  • 23
  • 20