Unable to add (+=) to associative array

I’m trying to increment a value [staCount] => 0 but keeps failing. Keep getting the msg:
Notice : Undefined index: staCount in ** on line 62

looking for ideas to fix this message… even thou its a notice its not incrementing the array value.

	foreach($loadMyDashboardDB as  $dbValue) {			
		foreach($dashboardArray as $pryKey => $pryValue){
			if($dbValue['tkt_priority'] == $pryValue['pryIndex']) {
				$dashboardArray[$pryKey]['pryCount'] += 1;				
/***************************************************************************** */
				foreach($dashboardArray as $staKey => $staValue) { 
					if($dbValue['tkt_status'] == $staValue[$staKey]['staIndex']) {
                                              \\ line 62
						**$dashboardArray[$staKey]['staCount'] += 1;**
					}
				}
/****************************************************************************** */
			}// END IF
		}// END FOREACH
	}// END FOREACH

Sample array:

Array
(
    [0] => Array
        (
            [pryIndex] => 1
            [pryTitle] => High
            [pryCount] => 1
            [0] => Array
                (
                    [staIndex] => 1
                    [staTitle] => Closed
                    [staCount] => 0
                )

            [1] => Array
                (
                    [staIndex] => 2
                    [staTitle] => Pending
                    [staCount] => 0
                )

foreach doesnt only walk the numeric elements of the array.
Think about what $staKey and $staValue would be for the very first iteration.

EDIT: Also think about what

is, as well.

1 Like

Hello M_htley,

Thank you for the reply but i’m going to need a little more help with the code.

Ty

foreach($dashboardArray as $staKey => $staValue) { 
  echo $staKey;
}

try that, and see what it tells you.

Because your (result) array is not indexed, you are doomed to loop over it for each row of database data. This is very inefficient. For the same reason you index database tables, you need to index the data in your array. This allows you to directly test and reference any element in the array. Consider the following -

<?php

// overall goal appears to be counting the number of tickets at each priority level and counting each status at each priority level

// define possible indexes
$tkt_priority = [1];
$tkt_status = [1,2];

// because some priority/status indexes may not exist in the actual data, define a starting array with all possibilities
$counts = [];
foreach($tkt_priority as $priIndex)
{
	foreach($tkt_status as $staIndex)
	{
		$counts[$priIndex]['total'] = 0; // note: if all status index values are included in the data, you don't need a separate total since the sum of the individual status values will be the total
		$counts[$priIndex][$staIndex] = 0;
	}
}

// examine the result
echo '<pre>'; print_r($counts);

// count the values in the data directly using an indexed array
foreach($loadMyDashboardDB as $row)
{
	$counts[ $row['tkt_priority'] ]['total'] += 1; // see note above about a separate total
	$counts[ $row['tkt_priority'] ][ $row['tkt_status'] ] += 1;
}

// examine the result
echo '<pre>'; print_r($counts);

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.