Reputation: 469
I have 2 Arrays. The First Array is a 1D Array which contains User ID's.
Something like this Array ( [0] => 1042800 [2] => 1184921 [4] => 724911 [66] => 1271106 )
I have a second Array which is an Associative Array, which contains Information for each User ID. It is something like this
Harvest_DayEntry::__set_state(array('user-id'=>1042800 'hours'=>2 ,Harvest_DayEntry::__set_state(array('user-id'=>1042800 'hours'=>4 ))
That was actually a part of the array. Now I am Interested in calculating the total hours for each User ID.
This is what I have tried.
//Add all the Hours for a Given User ID
//$output is the Array of User ID's
$resultGetEntries=$api->getProjectEntries($project_id,$range);
$dayEntries= $resultGetEntries->data;
$i=0;
$sum=$sum[20];
$sum= array_fill(0, 20, 0);
foreach ($dayEntries as $key => $value) {
if($output[i] == $value->get("user-id")) {
$sum[$i] = $sum[$i] + $value->get("hours");
}
$i++;
}
Eventually I want to get something like this
User ID Hours
1042800 15
1184921 18
724911 10
1271106 8
I am not too sure how to go about it. I can't seem to figure out how do I loop over an Associative array and calculate the sum for each user ID at the same time. I just need to know the proper logic to solve this problem.
Upvotes: 0
Views: 93
Reputation: 768
I assume $resultGetEntries
is your user ids array and $dayEntries
is the Harvest_DayEntry
s array.
The simplest way to combine both is iterarting over both in a double loop:
$resultGetEntries = $api->getProjectEntries($project_id,$range);
$dayEntries = $resultGetEntries->data;
$sums = array();
foreach ($resultGetEntries as $i => $userId) {
$sums[$userId] = 0;
foreach ($dayEntries as $j => $dayEntry) {
if ($dayEntry->get("user-id") == $userId) {
$sums[$userId] += $dayEntry->get("hours");
}
}
}
print_r($sums);
Upvotes: 1