10

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 group values as the key, then count how many are in each group to give the following:

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)

hakre
  • 193,403
  • 52
  • 435
  • 836
lethalMango
  • 4,433
  • 13
  • 54
  • 92
  • just to make sure, you are trying to get the `group` from the first array? –  Apr 18 '12 at 18:32
  • Yeah, so the range of groups from the first array (i.e. 1-3) as the keys of the new array and then count how many are in each group, again from the first array – lethalMango Apr 18 '12 at 18:34

6 Answers6

15

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

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:

$counts = array_count_values(array_flip(array_column($array, 'group')));
Michael Berkowski
  • 267,341
  • 46
  • 444
  • 390
5

This can be done with a simple array_map function

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

$array2 = (array_count_values($array));

print_r($array2);
Barmar
  • 741,623
  • 53
  • 500
  • 612
Vijaysinh Parmar
  • 897
  • 1
  • 15
  • 19
2

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']]++;
}
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
0

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>';
Alex
  • 11,115
  • 12
  • 51
  • 64
nimtek123
  • 1
  • 2
0
$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
)
sanriot
  • 804
  • 4
  • 13
tiya Ra
  • 1
  • 1
  • Is that *out-put result* or *out-put final*? Please describe why this is the way to solve the problem (compared to, e.g. the more functional approach of existing answers), and how yours works. – greybeard Mar 25 '20 at 09:00
0

You only need two native function calls for this task. Isolate the desired column of data with array_column(), then count the occurrences with array_count_values(). This is the only way that I would code this task in one of my projects.

The added benefit of using array_column() is that if some of your subarrays do not contain the targeted column key, then there will be no Notice/Warning generated. If you were to use a common looping technique or array_map/array_walk() instead, you might need to write conditional checks to prevent the Notices/Warnings.

Code: (Demo)

$array = [
    ['user' => 'Name 1', 'group' => 1],
    ['user' => 'Name 2', 'group' => 1],
    ['user' => 'Name 3', 'group' => 2],
    ['user' => 'Name 4', 'group' => 2],
    ['user' => 'Name 5', 'group' => 3],
];

var_export(
    array_count_values(array_column($array, 'group'))
);

Output:

array (
  1 => 2,
  2 => 2,
  3 => 1,
)
mickmackusa
  • 43,625
  • 12
  • 83
  • 136