现在,也许是因为我累了,但是在过去的一个小时里,我陷入了循环漩涡,试图以不同的方式(array_column()
,array_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行),其中array[0] = count
,array[1] = name
,array[2] = year
,array[3] = month
它基本上是一个人的所有git存储库中所有提交的汇总,目标是每个月每个项目的工作量分布近似。.
我需要了解的是MONTH
中每个YEAR
占NAME-COUNT
(每月总提交量)中MONTH-COUNT-OF-ALL-NAMES
(回购提交)的大概百分比
所以,我敢肯定有一个优雅的一线解决这个可怕的问题,但是我似乎再也无法做到这一点。
编辑我
我很确定自己所做的只会让每个人更加困惑,但是由于有评论提出,这是我重新排列数组的一些尝试:
该数组源自csv,因此
$csv = array_map('str_getcsv', file('git_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?>");
EDIT II
它基本上是一个人所有git存储库中所有提交的汇总,目标是每个月每个项目的工作量分布近似。.
我需要了解的是MONTH
中每个YEAR
占NAME-COUNT
(每月总提交量)中MONTH-COUNT-OF-ALL-NAMES
(回购提交)的大概百分比
对于示例数组,所需的输出类似于:
'2018-09 =>
array (
'repo_name' => '0m9-cart-main-app',
'commits' => 29,
'% of total commits for 2018-09 => 'x.xx%',
),
答案 0 :(得分:1)
首先找到所有唯一年份后,您就可以用MONTH-COUNT-OF-ALL-NAMES
来计算array_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_map
向数组中的每个条目添加一个百分比:
$data = array_map(function ($v) use ($mcoan) {
$v[4] = round($v[0]/$mcoan[$v[2]]*100,2);
return $v;
},
$data);
输出(对于您的小样本):
array (
0 =>
array (
0 => '29',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '9',
4 => 39.19,
),
1 =>
array (
0 => '16',
1 => '0m9-safe-box-server',
2 => '2017',
3 => '12',
4 => 39.02,
),
2 =>
array (
0 => '2',
1 => '0m9art-main-app-nodejs',
2 => '2017',
3 => '2',
4 => 4.88,
),
3 =>
array (
0 => '1',
1 => '0m9art-server-golang',
2 => '2017',
3 => '4',
4 => 2.44,
),
4 =>
array (
0 => '17',
1 => '0m9panel',
2 => '2017',
3 => '7',
4 => 41.46,
),
5 =>
array (
0 => '3',
1 => 'moli-server',
2 => '2017',
3 => '3',
4 => 7.32,
),
6 =>
array (
0 => '2',
1 => 'igcc',
2 => '2017',
3 => '11',
4 => 4.88,
),
7 =>
array (
0 => '26',
1 => '0m9-cart-main-app',
2 => '2108',
3 => '10',
4 => 35.14,
),
8 =>
array (
0 => '18',
1 => '0m9-safe-box-python-app',
2 => '2108',
3 => '12',
4 => 24.32,
),
9 =>
array (
0 => '1',
1 => '0m9art-evergreen-android-app',
2 => '2108',
3 => '5',
4 => 1.35,
),
)
答案 1 :(得分:1)
假设您的原始数组称为$ 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
。但是,如果您修改输入数据以生成适当的测试用例,则会看到数字发生相应的变化。
同样,不确定是否正是您想要的,在这方面的问题有点含糊。
答案 2 :(得分:1)
解决方案:
计算是使用两个数组进行的-一个用于每个周期的产品计数,另一个用于每个周期的总计数。
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>";
}
}
?>