My code looks like this:
$sql = $pdo->prepare('SELECT source_clicks,source_impr,source_spend FROM `statistic` WHERE camp_id =? AND date BETWEEN ? AND ?');
$sql->bindColumn('source_clicks',$source_clicks);
$sql->bindColumn('source_impr',$source_impr);
$sql->bindColumn('source_spend',$source_spend);
$sql->execute([$_POST['get_source_stat'],$date[0],$date[1]]);
while ( $sql->fetch()) {
$data = explode(',',$source_clicks);
print_r($data);
}
the output i get is:
Array ( [0] => 30 [1] => 30 [2] => 51 [3] => 108 ) Array ( [0] => 30 [1] => 30 [2] => 51 [3] => 228 )
i need to sum this arrays saving their keys and get something like this:
array ([0] => 60 [1] => 60 [2] => 102 [3] => 336)
Can you tell me how can i do it?
One way to do this would be to push each exploded $source_clicks value into an array, and then use array_sum and array_column to get the results e.g.
$data = array();
while ($sql->fetch()) {
$data[] = explode(',',$source_clicks);
}
$sums = array();
foreach (array_keys($data[0]) as $column) {
$sums[$column] = array_sum(array_column($data, $column));
}
print_r($sums);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With