I have an array of objects that looks like this:
Array
(
[0] => stdClass Object
(
[ContractorID] => 7049 //<- REMOVE
[ContractorName] => Supermarket 1 //<-REMOVE
[SpendAmount] => 615.36 //<-SUM OF ALL ELEMENTS WHERE GroupID IS THE SAME
[BonusAmount] => 24.61 //<-SUM OF ALL ELEMENTS WHERE GroupID IS THE SAME
[GroupID] => 1 //<- RETAIN
[GroupDescription] => Supermarkets //<- RETAIN
)
[1] => stdClass Object
(
[ContractorID] => 11233
[ContractorName] => Supermarket 2
[SpendAmount] => 858.74
[BonusAmount] => 34.35
[GroupID] => 1
[GroupDescription] => Supermarkets
)
[2] => stdClass Object
(
[ContractorID] => 19393
[ContractorName] => Car repair shop 1
[SpendAmount] => 386.79
[BonusAmount] => 15.47
[GroupID] => 2
[GroupDescription] => Automotive
)
[3] => stdClass Object
(
[ContractorID] => 19868
[ContractorName] => Mike's Autos
[SpendAmount] => 364.81
[BonusAmount] => 14.59
[GroupID] => 2
[GroupDescription] => Automotive
)
)
I want to end up with an array that looks like this:
[0] => stdClass Object
(
[SpendAmount] => 1474.1
[BonusAmount] => 58.96
[GroupID] => 1
[GroupDescription] => Supermarkets
)
[1] => stdClass Object
(
[SpendAmount] => 751.6
[BonusAmount] => 30.06
[GroupID] => 2
[GroupDescription] => Automotive
)
)
Where BonusAmount is the total of all elements BonusAmount and SpendAmount is the total of all elements SpendAmount.
The summing is simple enough in and of itself, but what I am struggling with is how to retain the GroupID and GroupDescription as I loop through the elements.
Could anyone give me an idea of how I should "retain" the GroupID and GroupDescription syntactically and create an element in my resultant array based on the GroupID? All help much appreciated.
Could anyone give me an idea. My idea - read a manual.