简体   繁体   English

php多维数组按类型和日期出现的次数

[英]php multidimensional array group number of occurrences by type and date

I have the following array: 我有以下数组:

Array
(
[0] => Array
    (
        [0] => 2015-07-18
        [1] => 22 SSH
    )

[1] => Array
    (
        [0] => 2015-07-18
        [1] => 80 HTTP
    )

[2] => Array
    (
        [0] => 2015-07-18
        [1] => 3389 Remote Desktop
    )

[3] => Array
    (
        [0] => 2015-07-19
        [1] => 3389 Remote Desktop
    )

[4] => Array
    (
        [0] => 2015-07-19
        [1] => 3389 Remote Desktop
    )
)

and need the data counted by day and number of occurrences in the following format: 并且需要按日期和出现次数计算的数据采用以下格式:

array(4) {
  [0]=>
  array(1) {
    [0]=> "3389 Remote Desktop"
    [1]=> "1,2"
  }
  [1]=>
  array(1) {
    [0]=> "22 SSH"
    [1]=> "1,0"
  }
  [2]=>
  array(1) {
    [0]=> "80 HTTP"
    [1]=> "1,0"
  }
}

How can I achieve this? 我怎样才能做到这一点? I am able to count all occurences but not grouped by date and type like this: 我可以计算所有出现但未按日期和类型分组,如下所示:

$counts = array();
foreach( $stack_stats_timeline as $value) {
    foreach( $value as $k => $v) {
        if( !isset( $counts[$k])) $counts[$k] = array();
        if( !isset( $counts[$k][$v])) $counts[$k][$v] = 0;
        $counts[$k][$v] += 1;
    }
}      

I think this'll help you to get it work 我想这会帮助你让它发挥作用

$result = array();
$count = 1;
foreach ($arr as $key => &$value) {
    $hash = $value[1];
    $result[$hash][0] = $value[1];
    $result[$hash][1][$value[0]] = (isset($result[$hash][1][$value[0]])) ? $count + 1 : $count;
}
print_r(array_values($result));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM