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?

7
  • What was the full error message? Commented Aug 21, 2012 at 8:48
  • Undefined offset: 1 in C:\xampp\htdocs\.... on line 479 , totally 5 error messages from Undefined offset: 1 to Undefined offset: 5 Commented Aug 21, 2012 at 8:51
  • What is the line of code on 479 Commented Aug 21, 2012 at 8:55
  • foreach($grades as $user_id=>$user_grade) Commented Aug 21, 2012 at 8:57
  • emm, in your given data set, your code works just fine for me. i suppose your $grades array has many more grades than just shown here? can you post the entire $grades array? Commented Aug 21, 2012 at 9:00

2 Answers 2

3

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
)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! but, with array_count_values, the grades without any users will be left out. I need it to be filled with zero if no user has gained this grade.
solved! I add $temp[$k] = isset($count[$k])?$count[$k]:0; to fill the empty grades.
0

Try It :

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

// $grades = array("User Id" => "Grade Value")
$grades = array( 123 => 1, 456 => 5 , 789 => 3);

//OutPUT :  array( "Gread Value " => "No Of Users ")

foreach($labels as $key => $value)
{
    $userCount = 0;
    foreach($grades as $key1=> $value1)
    {
        if($key == $value1)
        {
            $userCount++;
        }
    }

    $result[$key] = $userCount;
}

print_r($result);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.