for ($row = 0; $row < 25; $row++) {
$bQuery = 'Select date, billed from {$tableArray[$row][1]}';
$bProvider = new CSqlDataProvider($bQuery, array('pagination'=>false));
$Arr2 = $bProvider->getData();
// merging array to get all loop records in single array
$array_2 = array_merge($array_2, $Arr2);
}
Then print_r $array_2
output is:
Array
(
[0] => Array
(
[date] => 2016-11-00
[billed] => 100
)
[1] => Array
(
[date] => 2016-12-00
[billed] => 400
)
[2] => Array
(
[date] => 2016-11-00
[billed] => 200
)
// so on
)
I want to sum billed
where date is same
. So here is my code which works for it.
$result = array();
foreach ($array_2 as $data) {
@$result[$data['date']] += $data['billed'];
}
print_r($result);
$newresultGraph = array();
foreach ($result as $date => $billed) {
$newresultGraph[] = array('date' => $date, 'billed' => $billed);
}
print_r($newresultGraph);
The print_r($result)
in above codes returns me data like this below, It has already summed the values for the same date.
Array
(
[2016-11-00] => 300
[2016-12-00] => 400
)
Then in my above code after another foreach loop, Im making my data like before in array. when i print_r($newresultGraph)
, My data is like this.
Array
(
[0] => Array
(
[date] => 2016-11-00
[billed] => 300
)
[1] => Array
(
[date] => 2016-12-00
[billed] => 400
)
)
This is working perfectly for me.. because my query was this.
Select date, billed from table'
I was getting only date
, billed
from table but now i have another field so my query is like this..
Select
date,
billed,
((billed * rate) * (myValue) / 100) as my_rev,
((billed * rate) * (opValue) / 100) as op_rev,
From table
So i have two new values which are my_rev
and op_rev
Then print_r $array_2
output is:
Array
(
[0] => Array
(
[date] => 2016-11-00
[billed] => 100
[my_rev] => 30
[op_rev] => 70
)
[1] => Array
(
[date] => 2016-12-00
[billed] => 400
[my_rev] => 150
[op_rev] => 250
)
[2] => Array
(
[date] => 2016-11-00
[billed] => 200
[my_rev] => 50
[op_rev] => 150
)
// so on
)
which is correct,
I want to sum the values of all billed, my_rev, op_rev for a date.. I don't want to sum billed+my_rev+op_rev..
I want my array to be like this below: It should sum all the values of billed into billed, similarly for my_rev and op_rev.
Just like below example.
Array
(
[0] => Array
(
[date] => 2016-11-00
[billed] => 300
[my_rev] => 80
[op_rev] => 220
)
[1] => Array
(
[date] => 2016-12-00
[billed] => 400
[my_rev] => 150
[op_rev] => 250
)
)
My above code is turning my print_r($result)
into like this:
Array
(
[2016-11-00] => 600
[2016-12-00] => 800
)
What should i do? any other suggestion?
Just change your 2nd last foreach like this and then print the result array and see it will sum values with their keys.
$result = array();
foreach ($array_2 as $data) {
@$result[$data['date']]['billed'] += $data['billed'];
@$result[$data['date']]['my_rev'] += $data['my_rev'];
@$result[$data['date']]['op_rev'] += $data['op_rev'];
}
You can change your last foreach accordingly if you need it.
Result with your values:
Input:
Array
(
[0] => Array
(
[date] => 2016-11-00
[billed] => 100
[my_rev] => 30
[op_rev] => 70
)
[1] => Array
(
[date] => 2016-12-00
[billed] => 400
[my_rev] => 150
[op_rev] => 250
)
[2] => Array
(
[date] => 2016-11-00
[billed] => 200
[my_rev] => 50
[op_rev] => 150
)
)
Result:
Array
(
[2016-11-00] => Array
(
[billed] => 300
[my_rev] => 80
[op_rev] => 220
)
[2016-12-00] => Array
(
[billed] => 400
[my_rev] => 150
[op_rev] => 250
)
)