我正在尝试显示自开始使用此阵列以来每个广告系列号码的总安装量。
Array
(
[27] => Array
(
[0] => Array
(
[date] => 2018-04-09
[hour] => 7
[campaign] => 27
[installs] => 2
[trials] => 0
)
[1] => Array
(
[date] => 2018-04-09
[hour] => 3
[campaign] => 39
[installs] => 1
[trials] => 0
)
[2] => Array
(
[date] => 2018-04-09
[hour] => 5
[campaign] => 39
[installs] => 1
[trials] => 0
)
[3] => Array
(
[date] => 2018-04-09
[hour] => 6
[campaign] => 39
[installs] => 1
[trials] => 0
)
[4] => Array
(
[date] => 2018-04-09
[hour] => 8
[campaign] => 39
[installs] => 2
[trials] => 0
)
)
[53] => Array
(
[0] => Array
(
[date] => 2018-04-10
[hour] => 3
[campaign] => 53
[installs] => 1
[trials] => 0
)
[1] => Array
(
[date] => 2018-04-10
[hour] => 9
[campaign] => 53
[installs] => 1
[trials] => 0
)
[2] => Array
(
[date] => 2018-04-10
[hour] => 5
[campaign] => 55
[installs] => 1
[trials] => 0
)
[3] => Array
(
[date] => 2018-04-10
[hour] => 12
[campaign] => 55
[installs] => 1
[trials] => 0
)
)
[61] => Array
(
[0] => Array
(
[date] => 2018-04-15
[hour] => 0
[campaign] => 61
[installs] => 27
[trials] => 1
)
[1] => Array
(
[date] => 2018-04-15
[hour] => 1
[campaign] => 61
[installs] => 18
[trials] => 0
)
[2] => Array
(
[date] => 2018-04-15
[hour] => 1
[campaign] => 27
[installs] => 10
[trials] => 0
)
我的阵列如上所示。您可以看到某些广告系列号在不同日期重复显示。我的结果应该是这样的:
运动27:12
运动39:5
运动53:2
运动55:2
运动61:45
到目前为止,我已经有了这个,似乎并没有打印所有的数据。
$newarray = array();
$last = count($res) - 1;
foreach ($res as $i => $new){
foreach ($new as $x => $row){
$isFirst = ($i == 0);
$isLast = ($i == $last);
$grabbedvalue = $row['campaign'].",".$row['installs'];
array_push($newarray,$grabbedvalue);
}
}
print_r($newarray);
希望有人可以帮我解决这个问题。谢谢!
答案 0 :(得分:1)
只需遍历数组并计算:
$data = [
[
'campaign' => 1,
'installs' => 20,
],
[
'campaign' => 1,
'installs' => 5,
],
[
'campaign' => 2,
'installs' => 1,
],
[
'campaign' => 2,
'installs' => 10,
],
[
'campaign' => 3,
'installs' => 9,
],
];
$count=array();
// Loop through the campaigns
foreach($data as $camp){
// Check if we initiated the counting before
if(isset($count[$camp['campaign']])){
// Add installs count to previous value
$count[$camp['campaign']] += $camp['installs'];
} else {
// Initiate the counting
$count[$camp['campaign']] = $camp['installs'];
}
}
var_dump($count);
这将给出:
array (size=3)
1 => int 25
2 => int 11
3 => int 9
答案 1 :(得分:-1)
你应该在for循环中创建一个for循环来解决这个问题。
我可以尝试尽可能清楚地解释这些步骤: