Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中计算多维数组中值的出现次数_Php_Arrays - Fatal编程技术网

如何在php中计算多维数组中值的出现次数

如何在php中计算多维数组中值的出现次数,php,arrays,Php,Arrays,请原谅我的标题,因为我找不到合适的标题。 请看一下阵列 Array ( [0] => Array ( [0] => 2017 [1] => Array ( [0] => December ) ) [1] => Array ( [0] => 2017 [1] => Array

请原谅我的标题,因为我找不到合适的标题。 请看一下阵列

Array
(
[0] => Array
    (
        [0] => 2017
        [1] => Array
            (
                [0] => December
            )

    )

[1] => Array
    (
        [0] => 2017
        [1] => Array
            (
                [0] => December
            )

    )

[2] => Array
    (
        [0] => 2017
        [1] => Array
            (
                [0] => October
            )

    )

[3] => Array
    (
        [0] => 2016
        [1] => Array
            (
                [0] => December
            )

    )

 )
您可以看到2017年重复了三次,2017年12月内重复了两次。现在我想从这个数组中得到一个多维数组,它将显示2017年及其月份的发生情况

差不多

Array
(
  [2017] => 3
  [December]=> 2 // should be a nested array of 2017
  [October]=> 1 // should be a nested array of 2017
  [2016] => 1
  [December]=> 1 // should be a nested array of 2016
)
我尝试了
array\u count\u values
和一些自定义代码,但都成功了

Array
(
[2017] => 3
[2016] => 1
)
编辑:月份计数不必像这样键入。我所需要知道的是年份发生率和年份下的月份发生率


非常感谢您的帮助。谢谢

您可以尝试循环,然后检查年份键和月份键是否已经存在

$group = [];
foreach ($array as $value) {
    $month = $value[1][0];
    if (!isset($group[$value[0]])) {
        $group[$value[0]] = array('count' => 0);
    }
    if (!isset($group[$value[0]][$month])) {
        $group[$value[0]][$month] = 0;
    }
    $group[$value[0]][$month] += 1;
    $group[$value[0]]['count'] += 1;
}

print_r($group);
如果不一定需要计数,可以将第一个
If条件的执行更改为
$group[$value[0]]]=array()和行
$group[$value[0]['count']+=1

foreach ($array as $value) {
    $month = $value[1][0];
    if (!isset($group[$value[0]])) {
        $group[$value[0]] = array();
    }
    if (!isset($group[$value[0]][$month])) {
        $group[$value[0]][$month] = 0;
    }
    $group[$value[0]][$month] += 1;
}

print_r($group);

您可以尝试循环,然后检查年份键和月份键是否已经存在

$group = [];
foreach ($array as $value) {
    $month = $value[1][0];
    if (!isset($group[$value[0]])) {
        $group[$value[0]] = array('count' => 0);
    }
    if (!isset($group[$value[0]][$month])) {
        $group[$value[0]][$month] = 0;
    }
    $group[$value[0]][$month] += 1;
    $group[$value[0]]['count'] += 1;
}

print_r($group);
如果不一定需要计数,可以将第一个
If条件的执行更改为
$group[$value[0]]]=array()和行
$group[$value[0]['count']+=1

foreach ($array as $value) {
    $month = $value[1][0];
    if (!isset($group[$value[0]])) {
        $group[$value[0]] = array();
    }
    if (!isset($group[$value[0]][$month])) {
        $group[$value[0]][$month] = 0;
    }
    $group[$value[0]][$month] += 1;
}

print_r($group);

两个
[December]
键无法输入所需的结果所需的结果语法无效。如果您想将月份作为年下的嵌套数组,则计数还需要有一个键。Hi@Erwin它不必像这样设置键,我只需要月份在年下出现。Hi@Sean我没有得到这个“计数还需要有一个键”2
[December]
无法输入所需结果预期结果语法无效。如果你想将月份作为年下的嵌套数组,那么计数也需要有一个键。嗨@Erwin,它不需要像这样设置键,我只需要月份在年下出现。嗨@Sean,我没有像老板一样得到这个“计数也需要有一个键!”:)谢谢你老板这么好的解决方案!就像老板一样!:)谢谢你老板这么好的解决方案!