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

时间:2015-08-28 05:14:20

标签: php arrays sorting multidimensional-array

大家好我是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
        )

)

2 个答案:

答案 0 :(得分:1)

您只需使用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));

Demo

答案 1 :(得分:1)

以下是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));