Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7821733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:44:05+00:00 2026-06-02T07:44:05+00:00

I currently have the following array: Array( [0] => Array ( [user] => Name

  • 0

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']]++;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:44:07+00:00Added an answer on June 2, 2026 at 7:44 am

    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')));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following objects persisting successfully: Person first name, etc. Exams title,
I have the following program that writes a user's input to a file. Currently
I have the following array and I want to group all the users of
I have the following script currently, which gets the category IDs as arrays: $sql_select_categories
I currently have the following table definition: <table border=1 cellspacing=0 bordercolor=#CEDFEF cellpadding=1> I am
I currently have the following CSS for my 4 columns which might grow to
I currently have the following row in my table: course_data: user_id days <-- This
I currently have the following MySQL statement to replace the HTML entity for a
I currently have the following js code function clearMulti(option) { var i; var select
I currently have the following in one solution: Core Project (data access, biz logic,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.