I am trying to get the sum of all "amount" elements within my array based on customer and date range. Im wanting the output array (final result) to look something like this ...
Array
(
[0] => stdClass Object
(
[company_name] => Yeung's Architects
[customer_id] => 3
[name] => Billable Expenses Income
[due_date] => 2019-04-19 00:00:00
[amount] => 1543.64
[account_id] => 75
)
[1] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Billable Expenses Income
[due_date] => 2019-04-19 00:00:00
[amount] => 2177.27
[account_id] => 75
)
[2] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Billable Expenses Income
[due_date] => 2019-05-19 00:00:00
[amount] => 295.45
[account_id] => 75
)
)
This is the sql call that I am using ...
SELECT c.company_name
, i.customer_id
, a.name
, i.due_date
, li.amount
, li.account_id
FROM invoices i
JOIN customers c
ON c.customer_id = i.customer_id
JOIN invoices_line_items li
ON li.guid = i.guid
AND li.invoice_id = i.invoice_id
JOIN accounts a
ON a.guid = li.guid
AND a.account_id = li.account_id
WHERE i.guid = ?
AND i.date >= "'.$firstOfThisMonth.'" ',array($guid));
The resulting array from the sql call to start working with...
Array
(
[0] => stdClass Object
(
[company_name] => Yeung's Architects
[customer_id] => 3
[name] => Billable Expenses Income
[due_date] => 2019-04-19 00:00:00
[amount] => 1362.73
[account_id] => 75
)
[1] => stdClass Object
(
[company_name] => Yeung's Architects
[customer_id] => 3
[name] => Billable Expenses Income
[due_date] => 2019-04-19 00:00:00
[amount] => 180.91
[account_id] => 75
)
[2] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Billable Expenses Income
[due_date] => 2019-04-19 00:00:00
[amount] => 177.27
[account_id] => 75
)
[3] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Billable Expenses Income
[due_date] => 2019-05-19 00:00:00
[amount] => 159.09
[account_id] => 75
)
[4] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Billable Expenses Income
[due_date] => 2019-05-19 00:00:00
[amount] => 136.36
[account_id] => 75
)
[5] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Services
[due_date] => 2019-04-19 00:00:00
[amount] => 2000.00
[account_id] => 1
)
)
The code I am currently using to try and sum all of the amounts based on customer ID and the month of the invoice.
$output = array($data[0]); $c = 1;
foreach ($data as $key => $value) {
if ($key > 0) {
if (in_array($value->customer_id,array_column($output, 'customer_id'))) {
$valueDate = substr($value->due_date,0,7);
foreach ($output as $k => $v) {
if ($v->customer_id == $value->customer_id) {
$vDate = substr($v->due_date,0,7);
if ($vDate == $valueDate) {
$output[$k]->amount = $output[$k]->amount + $value->amount;
} else {
$output[$c] = $value;
$c++;
}
}
}
} else {
$output[$c] = $value;
$c++;
}
}
}
return $output;
This is my final results from the above mentioned code...
Array
(
[0] => stdClass Object
(
[company_name] => Yeung's Architects
[customer_id] => 3
[name] => Billable Expenses Income
[due_date] => 2019-04-19 00:00:00
[amount] => 1543.64
[account_id] => 75
)
[1] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Billable Expenses Income
[due_date] => 2019-04-19 00:00:00
[amount] => 2177.27
[account_id] => 75
)
[2] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Billable Expenses Income
[due_date] => 2019-05-19 00:00:00
[amount] => 295.45
[account_id] => 75
)
[3] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Billable Expenses Income
[due_date] => 2019-05-19 00:00:00
[amount] => 136.36
[account_id] => 75
)
[4] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Services
[due_date] => 2019-04-19 00:00:00
[amount] => 2000.00
[account_id] => 1
)
[5] => stdClass Object
(
[company_name] => Clement's Cleaners
[customer_id] => 8
[name] => Services
[due_date] => 2019-04-19 00:00:00
[amount] => 2000.00
[account_id] => 1
)
)
The first 3 keys are the correct results but then there are 3 extra keys added which Im not sure how they are being added. My brain is now broken, please may someone help me out, any help would be greatly appreciated.