我想根据日期对多维数组进行排序,并计算数组中数据出现的次数



大家好,我是php的新手。我尝试了很多事情来做到这一点,但我不能得到所需的输出。这是我的数组,我想对数组进行排序,计算数组中的月数,并在将所有数据按月推入新数组后计算数组中的数据出现次数,不。数据计数,键对。

    Array
(
    [0] => Array
        (
            [comp_date] => 2015/07/23 06:20
            [comp_status] => Lead
        )
    [1] => Array
        (
            [comp_date] => 2015/07/23 06:20
            [comp_status] => Lead
        )
    [2] => Array
        (
            [comp_date] => 2015/07/23 06:20
            [comp_status] => Opportunity
        )
    [3] => Array
        (
            [comp_date] => 2015/07/24 06:20
            [comp_status] => Conversion
        )
    [4] => Array
        (
            [comp_date] => 2015/07/24 06:20
            [comp_status] => Lead
        )
    [5] => Array
        (
            [comp_date] => 2015/08/24 06:20
            [comp_status] => Opportunity
        )
    [6] => Array
        (
            [comp_date] => 2015/08/24 06:20
            [comp_status] => Conversion
        )
    [7] => Array
        (
            [comp_date] => 2015/07/25 06:20
            [comp_status] => Lead
        )
    [8] => Array
        (
            [comp_date] => 2015/06/25 06:20
            [comp_status] => Opportunity
        )
    [9] => Array
        (
            [comp_date] => 2015/08/25 06:20
            [comp_status] => Conversion
        )
    [10] => Array
        (
            [comp_date] => 2015/08/25 06:20
            [comp_status] => Lead
        )
)

这是我实际的数组。我已经厌倦了这个

function yearWiseCalculation(){
$query = mysql_query("SELECT comp_date,comp_status FROM `hr_companies` WHERE  year(comp_date)=year(curdate())");
$arr =[];
while ($total = mysql_fetch_assoc($query)) {
    array_push($arr,$total);
    $ex = explode(" ", $total['comp_date']);
    $ex = explode(" ", $ex[0]);
    $k = explode("/", $ex[0]);
    $time[]=strtotime($ex[0]);
    $month[]=date("F",$time[0]);
}

我希望这个表单的输出如下

 Array(
       [0] => Array
        (
            [comp_month] => july
            [lead] => 4
            [opportunity] => 1
            [conversion] => 1
        )
      [1] => Array
        (
            [comp_month] => August
            [lead] => 1
            [opportunity] => 1
            [conversion] => 2
        )
)

您可以简单地使用foreach作为

$result = [];
foreach ($arr as $key => $value) {
    $hash = date('F', strtotime($value['comp_date']));
    if (isset($result[$hash])) {
        $result[$hash]['comp_month'] = $hash;
        $result[$hash]['lead'] = (isset($value['comp_status']) && $hash == $result[$hash]['comp_month'] && $value['comp_status'] == 'Lead') ? $result[$hash]['lead'] + 1 : $result[$hash]['lead'];
        $result[$hash]['opportunity'] = (isset($value['comp_status']) && $hash == $result[$hash]['comp_month'] && $value['comp_status'] == 'Opportunity') ? $result[$hash]['opportunity'] + 1 : $result[$hash]['opportunity'];
        $result[$hash]['conversion'] = (isset($value['comp_status']) && $hash == $result[$hash]['comp_month'] && $value['comp_status'] == 'Conversion') ? $result[$hash]['conversion'] + 1 : $result[$hash]['conversion'];
    } else {
        $result[$hash]['comp_month'] = date('F', strtotime($value['comp_date']));
        $result[$hash]['lead'] = ($value['comp_status'] == 'Lead') ? 1 : 0;
        $result[$hash]['opportunity'] = ($value['comp_status'] == 'Opportunity') ? 1 : 0;
        $result[$hash]['conversion'] = ($value['comp_status'] == 'Conversion') ? 1 : 0;
    }
}
uksort($result,function($a,$b){ return strtotime($a) - strtotime($b);});
print_r(array_values($result));
演示

foreach() + array_walk()版本:

foreach($array as $key => $row) {
    $dateKey                =   date("Y-m",strtotime(str_replace('/',"-",$row['comp_date']).':00'));
    $new[$dateKey][]        =   strtolower($row['comp_status']);
    $count[$dateKey]        =   array_count_values($new[$dateKey]);
}
array_walk($count,function(&$count,$k) {
    $count['comp_date']     =   date("F",strtotime($k));
    $count['conversion']    =   (empty($count['conversion']))? '0':$count['conversion'];
    $count['lead']          =   (empty($count['lead']))? '0':$count['lead'];
    $count['opportunity']   =   (empty($count['opportunity']))? '0':$count['opportunity'];
    ksort($count);
});
ksort($count,SORT_NATURAL);
print_r(array_values($count));

相关内容

  • 没有找到相关文章

最新更新