Php multidimensional number of groups of groups of occurrence 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 you need data calculated 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 can count all occurrences, but not grouped by date, and type type:

$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;
    }
}      
+4
source share
1 answer

I think it will help you make 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));
+1
source

Source: https://habr.com/ru/post/1599712/


All Articles