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

Php 我想根据日期对多维数组进行排序,并计算数组中出现的数据数,php,arrays,sorting,multidimensional-array,Php,Arrays,Sorting,Multidimensional Array,大家好,我是php的新手。我尝试了很多方法来实现这一点,但我无法获得所需的输出。 这是我的数组我想对数组进行排序,计算数组中的月数,并根据月数、数据计数数、键对将所有数据推送到新数组后计算数组中出现的数据 Array ( [0] => Array ( [comp_date] => 2015/07/23 06:20 [comp_status] => Lead ) [1] =&

大家好,我是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\u 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));

我尝试先根据日期对数据进行排序,然后从日期中提取月份,同时分解数组的第一个元素并将其推入新数组,但我无法计算数组中出现的月份数,也无法将它们分开,也无法根据月份对数据进行计数。