我正在尝试将高级别的产品评论数据插入到SKU记录中,但我仍然试图获得重复密钥的平均值。
Array
(
[0] => Array
(
[sku] => 70835
[rating] => 5
)
[1] => Array
(
[sku] => F6W/35
[rating] => 5
)
[2] => Array
(
[sku] => 36865
[rating] => 5
)
[3] => Array
(
[sku] => 36835
[rating] => 5
)
[4] => Array
(
[sku] => F30W/T8/830/POLYLUX
[rating] => 2
)
[5] => Array
(
[sku] => 70835
[rating] => 4
)
)
我想获得重复skus的平均评分,因此预期输出为:
Array
(
[0] => Array
(
[sku] => 70835
[rating] => 4.5
)
[1] => Array
(
[sku] => F6W/35
[rating] => 5
)
[2] => Array
(
[sku] => 36865
[rating] => 5
)
[3] => Array
(
[sku] => 36835
[rating] => 5
)
...
)
我有以下循环,它是对重复项进行求和,但我很难获得平均值
foreach ($reviews as $val) {
if (!isset($result[$val['sku']]))
{
$result[$val['sku']] = $val;
}
else{
$result[$val['sku']]['rating'] += $val['rating'];
#This will sum the duplicated ratings but I need to divide the sum here by the number of times the 'sku' index was duplicated so in the example 9/2 = 4.5
}
}
提前感谢!
答案 0 :(得分:1)
这应该适合你:
foreach ($reviews as $val) {
if (!isset($result[$val['sku']]))
{
$result[$val['sku']] = array('rating' => $val['rating'], 'count' => 1);
}
else{
$result[$val['sku']]['rating'] += $val['rating'];
$result[$val['sku']]['count']++;
}
}
foreach ($result as &$val) {
$val['average'] = $val['rating'] / $val['count'];
}
请注意,如果此数据来自数据库,则使用GROUP BY
语句可以更轻松地执行此操作。
答案 1 :(得分:1)
[akshay@localhost tmp]$ cat test.php
<?php
$array = array (
0 =>
array (
'sku' => '70835',
'rating' => '5',
),
1 =>
array (
'sku' => 'F6W/35',
'rating' => '5',
),
2 =>
array (
'sku' => '36865',
'rating' => '5',
),
3 =>
array (
'sku' => '36835',
'rating' => '5',
),
4 =>
array (
'sku' => 'F30W/T8/830/POLYLUX',
'rating' => '2',
),
5 =>
array (
'sku' => '70835',
'rating' => '4',
),
);
$final=$count=array();
foreach($array as $v)
{
if(isset($final[$v['sku']]))
{
$final[$v['sku']]['rating'] += $v['rating'];
$count[$v['sku']]++;
}else
{
$final[$v['sku']] = $v;
$count[$v['sku']] = 1;
}
}
array_map( function($a, $b) use (&$final){ $final[$a]['rating']/=$b; }, array_keys($count),array_values($count));
unset($count);
// Input
print_r($array);
// Output
print_r( array_values($final));
?>
<强> 输出 强>
[akshay@localhost tmp]$ php test.php
Array
(
[0] => Array
(
[sku] => 70835
[rating] => 5
)
[1] => Array
(
[sku] => F6W/35
[rating] => 5
)
[2] => Array
(
[sku] => 36865
[rating] => 5
)
[3] => Array
(
[sku] => 36835
[rating] => 5
)
[4] => Array
(
[sku] => F30W/T8/830/POLYLUX
[rating] => 2
)
[5] => Array
(
[sku] => 70835
[rating] => 4
)
)
Array
(
[0] => Array
(
[sku] => 70835
[rating] => 4.5
)
[1] => Array
(
[sku] => F6W/35
[rating] => 5
)
[2] => Array
(
[sku] => 36865
[rating] => 5
)
[3] => Array
(
[sku] => 36835
[rating] => 5
)
[4] => Array
(
[sku] => F30W/T8/830/POLYLUX
[rating] => 2
)
)
答案 2 :(得分:0)
如何为结果数组添加计数字段...
foreach ($reviews as $val) {
if (!isset($result[$val['sku']]))
{
$result[$val['sku']] = $val;
$result[$val['sku']]["count"] = 1;
}
else{
$result[$val['sku']]['rating'] += $val['rating'];
$result[$val['sku']]["count"] ++;
}
}
foreach($result as $k => $v) {
$result[$k]['avg'] = $v['rating']/$v['count'];
}