Getting values in the duplicate key values in array to add to each other

my code is like this

// $t = new ProjectProgressDao();
// $progressValues = $t->getRecords($projectId);

foreach ($progressValues as $Values) {

  $weekStartingDays[$this->CalculateWeekStartDate($Values->getDate())] = $Values->getWorkCompleted();
}

Basically what i want is???

 Array
    (
        [a1]=>'k'
        [a2]=>'a' 
        [a1]=>'w'
        [a1]=>'z'

     }

i want

 Array
    (
        [a1]=>'k+w+z'
        [a3]=>'w'
        [a4]=>'z'

     )

a,b,etc are numeric values

Upvotes: 0

Views: 78

Answers (1)

BMitch
BMitch

Reputation: 263896

Just add the value, initializing the position to 0 if it's not already set:

foreach ($progressValues as $Values) {
  if (!isset($weekStartingDays[$this->CalculateWeekStartDate($Values->getDate())])) {
    $weekStartingDays[$this->CalculateWeekStartDate($Values->getDate())]=0;
  }
  $weekStartingDays[$this->CalculateWeekStartDate($Values->getDate())] += $Values->getWorkCompleted();
}

Upvotes: 1

Related Questions