PHP中多维数组的平均结果 [英] Average result of multidimensional array in PHP
问题描述
这是所有阵列专家的问题.我有一个多维数组,其结果为数字(可以是0,1或2),并且需要每个按父级分组的平均值.
This is a question for all the array specialists out there. I have an multi dimension array with a result as number (can be 0,1 or 2) and need the average for each grouped by parent.
在下面的示例中,计算公式为:
In the example below the calculation would be:
subentry1_sub1 = 2 + 2 = 4(4/2 = 2 )
subentry1_sub1 = 2 + 2 = 4 (4/2=2)
subentry1_sub2 = 1 +1 = 2(2/2 = 1 )
subentry1_sub2 = 1 + 1 = 2 (2/2=1)
所以我尝试在PHP中归档的结果如下:
So what I try to archive in PHP is the following result:
subentry1_sub1平均= 2
subentry1_sub2平均值= 1
...
我已经尝试过类似问题的一些解决方案.但是,对于所有递归函数,我都无法按姓氏(例如subentry1_sub1)来汇总它.
I already tried some solutions from similar questions. But with all the recursive functions I didn't managed to get it aggregated by the last child name (e.g. subentry1_sub1).
有什么想法吗?
subentry1_sub1为2 + 2,因为它两次出现在数组中
subentry1_sub1 is 2 + 2 because its two times in the array
[entry1] => [subentry1] => [subentry1_sub1] =>结果
[entry1] => [subentry1] => [subentry1_sub1] => result
[entry2] => [subentry1] => [subentry1_sub1] =>结果
[entry2] => [subentry1] => [subentry1_sub1] => result
Array
(
[entry1] => Array
(
[subentry1] => Array
(
[subentry1_sub1] => Array
(
[value] => abc
[result] => 2
)
[subentry1_sub2] => Array
(
[value] => abc
[result] => 1
)
)
[subentry2] => Array
(
[subentry2_sub1] => Array
(
[value] => abc
[result] => 1
)
[subentry2_sub2] => Array
(
[value] => abc
[result] => 1
)
)
)
[entry2] => Array
(
[subentry1] => Array
(
[subentry1_sub1] => Array
(
[value] => abc
[result] => 2
)
[subentry1_sub2] => Array
(
[value] => abc
[result] => 1
)
)
[subentry2] => Array
(
[subentry2_sub1] => Array
(
[value] => abc
[result] => 1
)
[subentry2_sub2] => Array
(
[value] => abc
[result] => 1
)
)
)
)
推荐答案
尝试此代码.在此,我创建了一个新的 array
$ sum
,它将添加具有相同键的相同子项子级的结果值,以及另一个将添加 $ count
的数组计算每个按键重复的次数
Try this code. In this i have created a new array
$sum
which will add result value of same subentry childs with same key and another array $count
which will count the number of times each key repeats
<?php
$data = array('entry1'=>array(
'subentry1'=>
array(
'subentry1_sub1'=>array('value'=>'abc','result'=>2),
'subentry1_sub2'=>array('value'=>'abc','result'=>1)
),
'subentry2'=>
array(
'subentry2_sub1'=>array('value'=>'abc','result'=>1),
'subentry2_sub2'=>array('value'=>'abc','result'=>1)
)
),
'entry2'=>array(
'subentry1'=>
array(
'subentry1_sub1'=>array('value'=>'abc','result'=>2),
'subentry1_sub2'=>array('value'=>'abc','result'=>1)
),
'subentry2'=>
array(
'subentry2_sub1'=>array('value'=>'abc','result'=>1),
'subentry2_sub2'=>array('value'=>'abc','result'=>1)
)
)
);
$sum = array();
$repeat = array();
foreach($data as $input){
foreach($input as $array){
foreach($array as $key=>$value){
if(array_key_exists($key,$sum)){
$repeat[$key] = $repeat[$key]+1;
$sum[$key] = $sum[$key] + $value['result'];
}else{
$repeat[$key] = 1;
$sum[$key] = $value['result'];
}}}}
echo "<pre>";
print_r($sum);
print_r($repeat);
foreach($sum as $key=>$value){
echo $key. ' Average = '. $value/$repeat[$key]."</br>";
}
输出
Array
(
[subentry1_sub1] => 4
[subentry1_sub2] => 2
[subentry2_sub1] => 2
[subentry2_sub2] => 2
)
Array
(
[subentry1_sub1] => 2
[subentry1_sub2] => 2
[subentry2_sub1] => 2
[subentry2_sub2] => 2
)
subentry1_sub1 Average = 2
subentry1_sub2 Average = 1
subentry2_sub1 Average = 1
subentry2_sub2 Average = 1
您现在可以轻松计算平均值
You can easily calculate avg now
注意:正如您提到的,您正在计数 subentry1_sub1
等的发生,所以我也这样做了,因此也将 count
是否为键结果
是否存在
Note : As you mentioned you are counting occurence of subentry1_sub1
etc so i did the same so it will also count
whether key result
exists or not
这篇关于PHP中多维数组的平均结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!