Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Counting Values in Multidimensional Array

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)

like image 940
lethalMango Avatar asked Apr 18 '12 18:04

lethalMango


People also ask

How do you count values in an array?

You can simply use the PHP count() or sizeof() function to get the number of elements or values in an array. The count() and sizeof() function returns 0 for a variable that has been initialized with an empty array, but it may also return 0 for a variable that isn't set.

How do you count a 2D array in Python?

To get the length of a 2D Array in Python: Pass the entire array to the len() function to get the number of rows. Pass the first array element to the len() function to get the number of columns. Multiply the number of rows by the number of columns to get the total.

What is the use of count () function in PHP?

The count() function returns the number of elements in an array.


2 Answers

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')));
like image 119
Michael Berkowski Avatar answered Sep 28 '22 05:09

Michael Berkowski


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);
like image 23
Vijaysinh Parmar Avatar answered Sep 28 '22 04:09

Vijaysinh Parmar