If your sub arrays contain only 1 items, you can use following code.
array_count_values(array_map('key', $array));
Here,
array_count_values()Counts all the values of an arrayarray_map()Applies the callback to the elements of the given arrayskey()Fetch a key from an array
###Update
Update
As your are just looping its better you initialize $array before the loop and then add items to it. After the loop ends invoke array_count_values.
$array = array(); // initialize before loop
for(...){ /// sample loop
// your original code
$array[] = $grade; // add grades here
}
$grade_distribution = array_count_values($array); // count it
foreach($grade_distribution as $g => $count)
echo "score for $g = $count\n";
If your sub arrays contain only 1 items, you can use following code.
array_count_values(array_map('key', $array));
Here,
array_count_values()Counts all the values of an arrayarray_map()Applies the callback to the elements of the given arrayskey()Fetch a key from an array
###Update
As your are just looping its better you initialize $array before the loop and then add items to it. After the loop ends invoke array_count_values.
$array = array(); // initialize before loop
for(...){ /// sample loop
// your original code
$array[] = $grade; // add grades here
}
$grade_distribution = array_count_values($array); // count it
foreach($grade_distribution as $g => $count)
echo "score for $g = $count\n";
If your sub arrays contain only 1 items, you can use following code.
array_count_values(array_map('key', $array));
Here,
array_count_values()Counts all the values of an arrayarray_map()Applies the callback to the elements of the given arrayskey()Fetch a key from an array
Update
As your are just looping its better you initialize $array before the loop and then add items to it. After the loop ends invoke array_count_values.
$array = array(); // initialize before loop
for(...){ /// sample loop
// your original code
$array[] = $grade; // add grades here
}
$grade_distribution = array_count_values($array); // count it
foreach($grade_distribution as $g => $count)
echo "score for $g = $count\n";
If your sub arrays contain only 1 items, you can use following code.
array_count_values(array_map('key', $array));
Here,
array_count_values()Counts all the values of an arrayarray_map()Applies the callback to the elements of the given arrayskey()Fetch a key from an array
###Update
As your are just looping its better you initialize $array before the loop and then add items to it. After the loop ends invoke array_count_values.
$array = array(); // initialize before loop
for(...){ /// sample loop
// your original code
$array[] = $grade; // add grades here
}
$grade_distribution = array_count_values($array); // count it
foreach($grade_distribution as $g => $count)
echo "score for $g = $count\n";
If your sub arrays contain only 1 items, you can use following code.
array_count_values(array_map('key', $array));
Here,
array_count_values()Counts all the values of an arrayarray_map()Applies the callback to the elements of the given arrayskey()Fetch a key from an array
If your sub arrays contain only 1 items, you can use following code.
array_count_values(array_map('key', $array));
Here,
array_count_values()Counts all the values of an arrayarray_map()Applies the callback to the elements of the given arrayskey()Fetch a key from an array
###Update
As your are just looping its better you initialize $array before the loop and then add items to it. After the loop ends invoke array_count_values.
$array = array(); // initialize before loop
for(...){ /// sample loop
// your original code
$array[] = $grade; // add grades here
}
$grade_distribution = array_count_values($array); // count it
foreach($grade_distribution as $g => $count)
echo "score for $g = $count\n";
If your sub arrays contain only 1 items, you can use following code.
array_count_values(array_map('key', $array));
Here,
array_count_values()Counts all the values of an arrayarray_map()Applies the callback to the elements of the given arrayskey()Fetch a key from an array