获取所有月份的总和

时间:2018-10-30 10:24:57

标签: php

<?php
$log = array (
  array('AName'=>'Assessment','Year'=>'2018','Month'=>'09'),
  array('AName'=>'Assessment','Year'=>'2018','Month'=>'09'),
  array('AName'=>'Assessment','Year'=>'2018','Month'=>'09'),
  array('AName'=>'Assessment','Year'=>'2018','Month'=>'08'),
  array('AName'=>'Assessment','Year'=>'2018','Month'=>'07'),
  array('AName'=>'Linkage Programs','Year'=>'2018','Month'=>'09'),
  array('AName'=>'Housing Continuum of Care (CoC) Programs','Year'=>'2018','Month'=>'08'),
  array('AName'=>'Housing Continuum of Care (CoC) Programs','Year'=>'2018','Month'=>'09'),
  array('AName'=>'Recovery Homes','Year'=>'2018','Month'=>'08'),
  array('AName'=>'Recovery Homes','Year'=>'2018','Month'=>'08'),
  array('AName'=>'Recovery Homes','Year'=>'2018','Month'=>'09')
);
$data = array();
foreach($log as $event)
{
    if(isset($event['Month']))
    {
        $data[$event['Month']][$event['AName']] = (!isset($data[$event['Month']][$event['AName']])) ? 1 : $data[$event['Month']][$event['AName']] + 1;
    }
}
var_dump($data);
$totalArray = array();
foreach($data as $key => $value)
{
    foreach ($value as $keys => $values) 
  {
        echo $key.'-->'.$keys.'-->'.$values.'<br>';
  }  
}
?>

这是我的代码,我已经按月对AName进行了分组 我正在按月获取AName的计数,现在我想获取所有月份的AName的总和,例如,如果“评估”在11月为3,十月为2,则“评估”的总和应为5(总和)所有月份都使用AName)。请帮助

2 个答案:

答案 0 :(得分:0)

这是你想要的

您更新的代码我已经更新了您的代码。

<?php 
    $log = array (
      array('AName'=>'Assessment','Year'=>'2018','Month'=>'09'),
      array('AName'=>'Assessment','Year'=>'2018','Month'=>'09'),
      array('AName'=>'Assessment','Year'=>'2018','Month'=>'09'),
      array('AName'=>'Assessment','Year'=>'2018','Month'=>'08'),
      array('AName'=>'Assessment','Year'=>'2018','Month'=>'07'),
      array('AName'=>'Linkage Programs','Year'=>'2018','Month'=>'09'),
      array('AName'=>'Housing Continuum of Care (CoC) Programs','Year'=>'2018','Month'=>'08'),
      array('AName'=>'Housing Continuum of Care (CoC) Programs','Year'=>'2018','Month'=>'09'),
      array('AName'=>'Recovery Homes','Year'=>'2018','Month'=>'08'),
      array('AName'=>'Recovery Homes','Year'=>'2018','Month'=>'08'),
      array('AName'=>'Recovery Homes','Year'=>'2018','Month'=>'09')
    );
    echo "<pre>";
    $data = array();
    foreach($log as $event)
    {
        if(isset($event['Month']))
        {
            $data[$event['Month']][$event['AName']] = (!isset($data[$event['Month']][$event['AName']])) ? 1 : $data[$event['Month']][$event['AName']] + 1;
        }
    }
    print_r($data);
    $totalArray = array();
    foreach($data as $key => $value)
    {
        foreach ($value as $keys => $values) 
      {
            echo $key.'-->'.$keys.'-->'.$values.'<br>';
              if($keys=='Assessment'){
                  $sumAssessment[] = $values;
                 }else if($keys=='Linkage Programs'){
                            $sumLinkage[] = $values;
                 }else if($keys=="Housing Continuum of Care (CoC) Programs"){
                              $sumHousing[] = $values;
                            }else if($keys=='Recovery Homes'){
                              $sumRecovery[] = $values;
                            }
      }  
    }
    echo "Total Assessment=>".array_sum($sumAssessment).'<br>';
    echo "Total Linkage Programs=>".array_sum($sumLinkage).'<br>';
    echo "Total Housing Continuum of Care (CoC) Programs=>".array_sum($sumHousing).'<br>';
    echo "Total Recovery Homes=>".array_sum($sumRecovery);
?>

O / P

  Array
(
    [09] => Array
        (
            [Assessment] => 3
            [Linkage Programs] => 1
            [Housing Continuum of Care (CoC) Programs] => 1
            [Recovery Homes] => 1
        )

    [08] => Array
        (
            [Assessment] => 1
            [Housing Continuum of Care (CoC) Programs] => 1
            [Recovery Homes] => 2
        )

    [07] => Array
        (
            [Assessment] => 1
        )

)
09-->Assessment-->3
09-->Linkage Programs-->1
09-->Housing Continuum of Care (CoC) Programs-->1
09-->Recovery Homes-->1
08-->Assessment-->1
08-->Housing Continuum of Care (CoC) Programs-->1
08-->Recovery Homes-->2
07-->Assessment-->1
Total Assessment=>5
Total Linkage Programs=>1
Total Housing Continuum of Care (CoC) Programs=>2
Total Recovery Homes=>3 

答案 1 :(得分:0)

编写以下代码行:

$data2[$event['AName']] = isset($data2[$event['AName']]) ? $data2[$event['AName']] +1 : 1;

之后

$data[$event['Month']][$event['AName']] = (!isset($data[$event['Month']][$event['AName']])) ? 1 : $data[$event['Month']][$event['AName']] + 1;

Check here for output

O / P如

Array ( 
       [Assessment] => 5 
       [Linkage Programs] => 1 
       [Housing Continuum of Care (CoC) Programs] => 2 
       [Recovery Homes] => 3 
)