Php-排序现有数组和聚合键值
现在,也许是因为我累了,但在过去的一个小时里,我把自己陷入了一个循环漩涡,试图以不同的方式排列数组(Php-排序现有数组和聚合键值,php,arrays,sorting,Php,Arrays,Sorting,现在,也许是因为我累了,但在过去的一个小时里,我把自己陷入了一个循环漩涡,试图以不同的方式排列数组(array\u column(),array\u map(),以及少量的foreach()和if())-这个漩涡的结果是如此混乱,以至于我再也看不到森林里的树木了,坦率地说-我甚至羞于发布我尝试过的意大利面代码:-) 该数组如下所示: array ( 0 => array ( 0 => '29', 1 => '0m9-cart-main-app',
array\u column()
,array\u map()
,以及少量的foreach()
和if()
)-这个漩涡的结果是如此混乱,以至于我再也看不到森林里的树木了,坦率地说-我甚至羞于发布我尝试过的意大利面代码:-)
该数组如下所示:
array (
0 =>
array (
0 => '29',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '9',
),
1 =>
array (
0 => '16',
1 => '0m9-safe-box-server',
2 => '2017',
3 => '12',
),
2 =>
array (
0 => '2',
1 => '0m9art-main-app-nodejs',
2 => '2017',
3 => '2',
),
3 =>
array (
0 => '1',
1 => '0m9art-server-golang',
2 => '2017',
3 => '4',
),
4 =>
array (
0 => '17',
1 => '0m9panel',
2 => '2017',
3 => '7',
),
5 =>
array (
0 => '3',
1 => 'moli-server',
2 => '2017',
3 => '3',
),
6 =>
array (
0 => '2',
1 => 'igcc',
2 => '2017',
3 => '11',
),
7 =>
array (
0 => '26',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '10',
),
8 =>
array (
0 => '18',
1 => '0m9-safe-box-python-app',
2 => '2108',
3 => '12',
),
9 =>
array (
0 => '1',
1 => '0m9art-evergreen-android-app',
2 => '2108',
3 => '5',
), ......
数组继续运行(大约700行),其中数组[0]=计数
,数组[1]=名称
,数组[2]=年
,数组[3]=月
它基本上是一个人在所有git存储库中的所有提交的聚合,目标是每个月有一个每个项目工作负载分布的近似值
我需要知道的是,对于年的每个月
,名称计数
(回购承诺)与所有名称的总月份计数
(每月总承诺)的近似百分比是多少
所以,我相信有一个优雅的一行解决这个可怕的数组,但我似乎不能再这样做了
编辑I
我敢肯定,我所做的只会让大家更加困惑,但既然有评论提出了问题,以下是我在重新安排阵列方面的一些尝试:
该数组源于csv,因此
$csv=array\u map('str\u getcsv',file('git\u stati.csv')代码>
给出上面发布的原始数组
`
// try I
foreach($csv as $line){
$i=1;
// $r_date[] = $line[2] . $line[3] ;
$r_date = $line[2] .'-'. $line[3] ;
$r_dater[$r_date.'-'.$line[1] ] = $line[0] ;
$r_new[$line[1]]= $line[0] ;
$i++;
}
`// try II
foreach($csv as $line){
if ( $line[2] == '2018' ){
$name[] = $line[1] ;
$count[$line[1] ] += $line[0];
}
if ( $line[2] == '2017' ){
$name[] = $line[1] ;
$count[$line[1] ] += $line[0];
}
}
// try III
// foreach($r_dater as $key => $val) {
// if(substr($key, 0, 6) == '2018-9'){
// $str2 = substr($key, 7);
// $special_items[$key] = $val;
// $repo_r[$str2]= $val;
// }
// }
// other failed confusing trials ...
highlight_string("<?php\n\$data =\n". var_export($r_dater, true) . ";\n?>");
`
//试试我
foreach($csv作为$line){
$i=1;
//$r_date[]=第$line[2]。$line[3];
$r_date=$line[2].-'.$line[3];
$r_dater[$r_date.'-'.$line[1]]=$line[0];
$r_new[$line[1]]=$line[0];
$i++;
}
`//试试看
foreach($csv作为$line){
如果($line[2]=“2018”){
$name[]=$line[1];
$count[$line[1]]+=$line[0];
}
如果($line[2]=“2017”){
$name[]=$line[1];
$count[$line[1]]+=$line[0];
}
}
//第三次尝试
//foreach($r_dater as$key=>$val){
//如果(substr($key,0,6)=‘2018-9’){
//$str2=substr($key,7);
//$special_items[$key]=$val;
//$repo_r[$str2]=$val;
// }
// }
//其他失败的令人困惑的审判。。。
突出显示_字符串(“”);
编辑II
它基本上是一个人在所有git存储库中的所有提交的聚合,目标是每个月有一个每个项目工作负载分布的近似值
我需要知道的是,对于年的每个月
,名称计数
(回购承诺)与所有名称的总月份计数
(每月总承诺)的近似百分比是多少
对于示例阵列,所需的输出类似于:
'2018-09=>
排列(
“回购名称”=>“0m9购物车主应用程序”,
“提交”=>29,
2018-09年提交总数的%=>x.xx%,
),
在首次找到所有唯一年份后,您可以使用数组\u reduce
计算所有姓名的月计数:
$years = array_unique(array_column($data, 2));
$mcoan = array_reduce($data,
function ($c, $d) {
$c[$d[2]] += (int)$d[0];
return $c;
},
array_combine($years, array_fill(0, count($years), 0)));
这将生成如下数组:
Array ( [2108] => 74 [2017] => 41 )
然后,您可以使用array\u map
向数组中的每个条目添加一个百分比:
$data = array_map(function ($v) use ($mcoan) {
$v[4] = round($v[0]/$mcoan[$v[2]]*100,2);
return $v;
},
$data);
输出(对于小样本):
首先查找所有唯一年份后,您可以使用array\u reduce
计算您的MONTH-COUNT-OF-ALL-NAMES
:
$years = array_unique(array_column($data, 2));
$mcoan = array_reduce($data,
function ($c, $d) {
$c[$d[2]] += (int)$d[0];
return $c;
},
array_combine($years, array_fill(0, count($years), 0)));
这将生成如下数组:
Array ( [2108] => 74 [2017] => 41 )
然后,您可以使用array\u map
向数组中的每个条目添加一个百分比:
$data = array_map(function ($v) use ($mcoan) {
$v[4] = round($v[0]/$mcoan[$v[2]]*100,2);
return $v;
},
$data);
输出(对于小样本):
假设您的原始数组名为$data(并且我理解您想从什么开始,但不完全确定),您可以这样做:
// sum up values for year and month in two-dimensional array
$sums = [];
foreach($data as $item) {
if(isset($sums[$item[2]][$item[3]])) {
$sums[$item[2]][$item[3]] += $item[0]; // add value, if entry for year and month already exists,
}
else {
$sums[$item[2]][$item[3]] = $item[0]; // otherwise assign as initial value
}
}
// update original array, adding calculated percentage as $item[4]
foreach($data as &$item) {
$item[4] = ($item[0] / $sums[$item[2]][$item[3]] * 100);
}
unset($item); // working with a reference above, so don’t forget to unset
var_dump($data);
现在,对于您显示的示例数据,这只会导致每个项目的值100
,因为首先,每年每月的组合不超过一个条目。但是,如果您修改输入数据以生成适当的测试用例,您将看到数字相应地发生变化
同样,我不确定这是否正是您想要的,这个问题在这方面有点含糊不清。假设您的原始数组名为$data(并且我理解您想从什么开始,但不完全确定),您可以这样做:
// sum up values for year and month in two-dimensional array
$sums = [];
foreach($data as $item) {
if(isset($sums[$item[2]][$item[3]])) {
$sums[$item[2]][$item[3]] += $item[0]; // add value, if entry for year and month already exists,
}
else {
$sums[$item[2]][$item[3]] = $item[0]; // otherwise assign as initial value
}
}
// update original array, adding calculated percentage as $item[4]
foreach($data as &$item) {
$item[4] = ($item[0] / $sums[$item[2]][$item[3]] * 100);
}
unset($item); // working with a reference above, so don’t forget to unset
var_dump($data);
现在,对于您显示的示例数据,这只会导致每个项目的值100
,因为首先,每年每月的组合不超过一个条目。但是,如果您修改输入数据以生成适当的测试用例,您将看到数字相应地发生变化
同样,不确定这是否正是您想要的,这个问题在这方面有点模糊。解决方案:
<?php
# Input
$input = array(
array (
0 => '20',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '9',
),
array (
0 => '30',
1 => '0m9art-main-app-nodejs',
2 => '2108',
3 => '9',
),
array (
0 => '20',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '10',
),
array (
0 => '2',
1 => '0m9art-main-app-nodejs',
2 => '2108',
3 => '10',
)
);
# Calculate product and total counts
$monthProducts = array();
$monthTotal = array();
foreach($input as $item) {
$count = $item[0];
$name = $item[1];
$year = $item[2];
$month = $item[3];
$period = $year.'-'.$month;
if (!array_key_exists($period, $monthTotal)) {
$monthTotal[$period] = 0;
}
$monthTotal[$period] += $count;
if (!array_key_exists($period, $monthProducts)) {
$monthProducts[$period] = array();
}
if (!array_key_exists($name, $monthProducts[$period])) {
$monthProducts[$period][$name] = 0;
}
$monthProducts[$period][$name] += $count;
}
# Approximate percentage and output by period
foreach($monthProducts as $period => $products) {
echo $period."<br>";
foreach($products as $name => $count) {
echo "Product '". $name. "' approximate percentage: ". round($count / $monthTotal[$period] * 100, 2), " %. <br>";
}
}
?>
使用两个数组进行计算-一个用于每个周期的产品计数,另一个用于每个周期的总计数
PHP:
<?php
# Input
$input = array(
array (
0 => '20',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '9',
),
array (
0 => '30',
1 => '0m9art-main-app-nodejs',
2 => '2108',
3 => '9',
),
array (
0 => '20',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '10',
),
array (
0 => '2',
1 => '0m9art-main-app-nodejs',
2 => '2108',
3 => '10',
)
);
# Calculate product and total counts
$monthProducts = array();
$monthTotal = array();
foreach($input as $item) {
$count = $item[0];
$name = $item[1];
$year = $item[2];
$month = $item[3];
$period = $year.'-'.$month;
if (!array_key_exists($period, $monthTotal)) {
$monthTotal[$period] = 0;
}
$monthTotal[$period] += $count;
if (!array_key_exists($period, $monthProducts)) {
$monthProducts[$period] = array();
}
if (!array_key_exists($name, $monthProducts[$period])) {
$monthProducts[$period][$name] = 0;
}
$monthProducts[$period][$name] += $count;
}
# Approximate percentage and output by period
foreach($monthProducts as $period => $products) {
echo $period."<br>";
foreach($products as $name => $count) {
echo "Product '". $name. "' approximate percentage: ". round($count / $monthTotal[$period] * 100, 2), " %. <br>";
}
}
?>
解决方案:
<?php
# Input
$input = array(
array (
0 => '20',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '9',
),
array (
0 => '30',
1 => '0m9art-main-app-nodejs',
2 => '2108',
3 => '9',
),
array (
0 => '20',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '10',
),
array (
0 => '2',
1 => '0m9art-main-app-nodejs',
2 => '2108',
3 => '10',
)
);
# Calculate product and total counts
$monthProducts = array();
$monthTotal = array();
foreach($input as $item) {
$count = $item[0];
$name = $item[1];
$year = $item[2];
$month = $item[3];
$period = $year.'-'.$month;
if (!array_key_exists($period, $monthTotal)) {
$monthTotal[$period] = 0;
}
$monthTotal[$period] += $count;
if (!array_key_exists($period, $monthProducts)) {
$monthProducts[$period] = array();
}
if (!array_key_exists($name, $monthProducts[$period])) {
$monthProducts[$period][$name] = 0;
}
$monthProducts[$period][$name] += $count;
}
# Approximate percentage and output by period
foreach($monthProducts as $period => $products) {
echo $period."<br>";
foreach($products as $name => $count) {
echo "Product '". $name. "' approximate percentage: ". round($count / $monthTotal[$period] * 100, 2), " %. <br>";
}
}
?>
使用两个数组进行计算-一个用于每个周期的产品计数,另一个用于每个周期的总计数
PHP:
<?php
# Input
$input = array(
array (
0 => '20',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '9',
),
array (
0 => '30',
1 => '0m9art-main-app-nodejs',
2 => '2108',
3 => '9',
),
array (
0 => '20',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '10',
),
array (
0 => '2',
1 => '0m9art-main-app-nodejs',
2 => '2108',
3 => '10',
)
);
# Calculate product and total counts
$monthProducts = array();
$monthTotal = array();
foreach($input as $item) {
$count = $item[0];
$name = $item[1];
$year = $item[2];
$month = $item[3];
$period = $year.'-'.$month;
if (!array_key_exists($period, $monthTotal)) {
$monthTotal[$period] = 0;
}
$monthTotal[$period] += $count;
if (!array_key_exists($period, $monthProducts)) {
$monthProducts[$period] = array();
}
if (!array_key_exists($name, $monthProducts[$period])) {
$monthProducts[$period][$name] = 0;
}
$monthProducts[$period][$name] += $count;
}
# Approximate percentage and output by period
foreach($monthProducts as $period => $products) {
echo $period."<br>";
foreach($products as $name => $count) {
echo "Product '". $name. "' approximate percentage: ". round($count / $monthTotal[$period] * 100, 2), " %. <br>";
}
}
?>
在问题中输入您已尝试过的内容是否应每年计算所有姓名的月计数
?将其循环一次,以汇总每年/月组合的计数,并将其放入新数组中。然后,第二次循环原始数组,以计算项目给定值相对于刚计算的年/月总金额的百分比…是否修改原始数组以包含该百分比值,或将其写入新数组-由您决定…(您没有指定)在问题中输入您尝试过的内容是否应每年计算所有姓名的月计数?循环一次,总结每个姓名的计数