php 计算多维数组中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10215659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:40:26  来源:igfitidea点击:

Counting Values in Multidimensional Array

phparraysforeachcounting

提问by lethalMango

I currently have the following array:

我目前有以下数组:

Array(
        [0] => Array
            (
                [user] => Name 1
                [group] => 1
            )
        [1] => Array
            (
                [user] => Name 2
                [group] => 1
            )
        [2] => Array
            (
                [user] => Name 3
                [group] => 2
            )
        [3] => Array
            (
                [user] => Name 4
                [group] => 2
            )
        [4] => Array
            (
                [user] => Name 5
                [group] => 3
            )
)

I am attempting to create a new array with the various groupvalues as the key, then count how many are in each group to give the following:

我正在尝试创建一个以各种group值为键的新数组,然后计算每个组中有多少个以给出以下内容:

Array
(
    [1] => 2
    [2] => 2
    [3] => 1
)

I have attempted to use the following, however I get undefined index warnings:

我尝试使用以下内容,但是我收到未定义的索引警告:

$newArr = array();
foreach ($details['user_groups'] as $key => $value) {
        $newArr[$value['user_groups']]++;
}

(I did check SO for other answers, however couldn't find one attempting to do the same)

(我确实检查了其他答案,但是找不到试图做同样的事情)

回答by Michael Berkowski

This can be done with a simple iteration:

这可以通过一个简单的迭代来完成:

$counts = array();
foreach ($array as $key=>$subarr) {
  // Add to the current group count if it exists
  if (isset($counts[$subarr['group']]) {
    $counts[$subarr['group']]++;
  }
  // or initialize to 1 if it doesn't exist
  else $counts[$subarr['group']] = 1;

  // Or the ternary one-liner version 
  // instead of the preceding if/else block
  $counts[$subarr['group']] = isset($counts[$subarr['group']]) ? $counts[$subarr['group']]++ : 1;
}

Update for PHP 5.5

PHP 5.5 更新

In PHP 5.5, which has added the array_column()function to aggregate an inner key from a 2D array, this can be simplified to:

在 PHP 5.5 中,它添加了array_column()从 2D 数组聚合内部键的函数,这可以简化为:

$counts = array_count_values(array_flip(array_column($array, 'group')));

回答by Vijaysinh Parmar

This can be done with a simple array_mapfunction

这可以通过一个简单的array_map函数来完成

$array = array_map(function($element){
    return $element['group'];
}, $array1);

$array2 = (array_count_values($array));

print_r($array2);

回答by FtDRbwLXw6

Your initial attempt was close. You were simply using the wrong key inside the loop:

您最初的尝试已接近尾声。您只是在循环中使用了错误的键:

$newArr = array();
foreach ($details['user_groups'] as $key => $value) {
        // What you were using:
        // $newArr[$value['user_groups']]++;

        // What you should be using:
        $newArr[$value['group']]++;
}

回答by nimtek123

Try this simple but effective way

试试这个简单但有效的方法

$count = call_user_func_array('array_merge_recursive', $Array);
echo count($count['user']).'<br>';
echo count($count['group']).'<br>';

回答by tiya Ra

$output = array();
  foreach($data as $key => $value){
    $output[$value['group']]['count']++;
}

$final = array();
foreach($output as $key => $value){
    $final[$key] = $value['count'];    
}

print_r($final);
//out put result:=========
Array(
    [1] => 2
    [2] => 2
    [3] => 1
)