Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中按多个键对数组进行分组并对结果求和_Php_Arrays - Fatal编程技术网

如何在php中按多个键对数组进行分组并对结果求和

如何在php中按多个键对数组进行分组并对结果求和,php,arrays,Php,Arrays,对不起,各位,我是这个网站的新手,我不知道如何正确设置这个问题的格式。 我有一个数组(),它包含大约6000个项目。例如,每个项目有3个属性 Item[1]user=>'user1', acct_num => '1', value => 32, Item[2]user=>'user1', acct_num => '1', value => 2, Item[3]user=>'user1', acct_num => '2', value => 3

对不起,各位,我是这个网站的新手,我不知道如何正确设置这个问题的格式。 我有一个数组(),它包含大约6000个项目。例如,每个项目有3个属性

Item[1]user=>'user1', acct_num => '1', value => 32,
Item[2]user=>'user1', acct_num => '1', value => 2,
Item[3]user=>'user1', acct_num => '2', value => 32,
Item[5]user=>'user1', acct_num => '2', value => 32,
Item[6]user=>'user2', acct_num => '1', value => 312,
Item[1]user=>'user2', acct_num => '1', value => 320,

所以我想得到账户1的用户1的总数,在这个例子中是34,我需要账户2的用户1的总数是64,然后两个总数加起来是98,然后将账户总数除以整个总数(94)。因此将是34/98和64/98。我需要对数组中的所有项执行此操作

如果您不想利用数据库,只需对数据集进行迭代、分组并计算平均值即可

$csv=[
['user'=>'user1','acct_num'=>'1','value'=>32],
['user'=>'user1','acct_num'=>'1','value'=>2],
['user'=>'user1','acct_num'=>'2','value'=>32],
['user'=>'user1','acct_num'=>'2','value'=>32],
['user'=>'user2','acct_num'=>'1','value'=>312],
['user'=>'user2','acct_num'=>'1','value'=>320],
];
$小计=[];//每个帐户的总数
$totals=[];//每用户总数
$results=[];
foreach($csv作为$row){
列表($user,$acct\u num,$value)=数组值($row);
$subtotal=isset($subtotals[$user][$acct\u num])?$subtotals[$user][$acct\u num]:0;
$subtotal[$user][$acct_num]=$subtotal+$value;
$total=isset($totals[$user])?$totals[$user]:0;
$totals[$user]=$total+$value;
}
foreach($user=>$acct\u nums小计){
foreach($acct\u nums为$acct\u num=>$value){
$total=$totals[$user];
$avg=$value/$total;
$results[]=compact('user','acct_num','value','total','avg');
}
}
打印(结果);
这是一个

输出:

Array ( [0] => Array ( [user] => user1 [acct_num] => 1 [value] => 34 [total] => 98 [avg] => 0.3469387755102 ) [1] => Array ( [user] => user1 [acct_num] => 2 [value] => 64 [total] => 98 [avg] => 0.6530612244898 ) [2] => Array ( [user] => user2 [acct_num] => 1 [value] => 632 [total] => 632 [avg] => 1 ) ) 大堆 ( [0]=>阵列 ( [用户]=>user1 [acct_num]=>1 [值]=>34 [总数]=>98 [平均值]=>0.346938775102 ) [1] =>阵列 ( [用户]=>user1 [acct_num]=>2 [值]=>64 [总数]=>98 [平均值]=>0.6530612244898 ) [2] =>阵列 ( [用户]=>用户2 [acct_num]=>1 [值]=>632 [总计]=>632 [平均值]=>1 ) )
你有没有可能从数据库中获取此信息?我从csv文件中获取阵列,我试图避开数据库。因为在最后,我将把最后一个数组插入到数据库中,并将其导出为xlsShow us您的代码。到目前为止,你尝试了什么?老实说,我没有写任何东西,因为我不知道如何继续,显然php没有本地的东西来进行排序。到目前为止,我正在做一个糟糕的工作流程,比如将整个csv文件插入数据库,并按用户帐户和用户和帐户进行选择,以及按用户和帐户从报告组中选择用户、帐户、和(值)。但是我想放弃数据库,做数组操作,最后插入数据库这太棒了!我想知道你是如何做到这一点的。我想这里有一个意外的错误。对于数组中的每个项目,总计重复两次。