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

  • Home
  • SEARCH
  • 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 8410851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:17:13+00:00 2026-06-10T00:17:13+00:00

I have one array stores grade names, with grade value as key and grade

  • 0

I have one array stores grade names, with grade value as key and grade name as value:

$labels=array(1=>'Insufficient',2=>'Sufficient',3=>'Satisfactory',4=>'Good',5=>'Excellent' );

Then, there’s another array stores all record of people who gained a grade, with user_id as key and grade value as value:

$grades = array( 123 = > 1, 456 => 5 , 789 = > 3);

Now I want to count the record under each grade– grade-1 => 5 users, grade-2=>6 users, in the form like this:

array(1=>5, 2=>6, 3=>2, 4=>2, 5=>1);

I tried this and got errors of Undefined offset:

$result = array();

for($i=1;$i<=5;++$i)
{
    foreach($grades as $user_id=>$user_grade)
    {
        $result[$i] = ( $user_grade == $i ) ? $result[$i]+1 : $result[$i];
    }
}

How to get an result array of counting how many user under each grade?

  • 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-10T00:17:14+00:00Added an answer on June 10, 2026 at 12:17 am

    Sounds like array_count_values() would do the trick.

    e.g.

    <?php
    $grades = array ( // this array contains no value "1"
       68 => 4, 138 => 4, 189 => 2, 255 => 4,
      297 => 3, 366 => 2, 425 => 4, 465 => 3,
      489 => 5, 580 => 4, 646 => 4, 704 => 5,
      784 => 2, 811 => 4, 897 => 4, 974 => 5,
      1006 => 2, 1093 => 2, 1189 => 2, 1222 => 4
    );
    
    $cnt = array_count_values($grades);
    $cnt += array(1=>0,0,0,0,0);
    ksort($cnt);
    
    print_r($cnt);
    

    prints

    Array
    (
        [1] => 0
        [2] => 6
        [3] => 2
        [4] => 9
        [5] => 3
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one array. I want that array to retain its value between function
I have a function which must return one dimensional associate array, like $user_info[$index]=value where
I have an array that stores names of countries (keys) and age expectancy (values).
I have one array that is filled by mysql_query. Values that are in this
lets say i have one array a = numpy.arange(8*6*3).reshape((8, 6, 3)) #and another: l
I want to implement singleten design pattern in iphone code I have one array.
I have one main array which is the returned data from a MySQL query
i have one php file which process adding of record in Database fro array.
for example: I have created one javascript property array model with some properties like
I have a for loop that loops over one array... for i=1:length(myArray) In this

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.