Sum first two results, then the next two
I'm trying to make sum of first two results from MySQL query. Since I need to sum of the first two columns, I need to take the result of the previous and the sum with the next one following result.
I tried to use static keyword but was not work as expected.
$sum = 0;
foreach($test as $key=>$value){
static $q;
$q = $sum+= $value;
}
echo $q;
Schema:
When I var_dump($test) results are: array(1) { [0]=> string(5) "21.00" } array(2) { [0]=> string(5) "21.00" 1=> string(5) "19.00" } array(3) { [0]=> string(5) "21.00" 1=> string(5) "19.00" [2]=> string(5) "24.00" } array(4) { [0]=> string(5) "21.00" 1=> string(5) "19.00" etc..
I tried to use static keyword but was not work as expected.
$sum = 0;
foreach($test as $key=>$value){
static $q;
$q = $sum+= $value;
}
echo $q;
Schema:
When I var_dump($test) results are: array(1) { [0]=> string(5) "21.00" } array(2) { [0]=> string(5) "21.00" 1=> string(5) "19.00" } array(3) { [0]=> string(5) "21.00" 1=> string(5) "19.00" [2]=> string(5) "24.00" } array(4) { [0]=> string(5) "21.00" 1=> string(5) "19.00" etc..
Комментарии
Отправить комментарий