Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Commonmark migration
Source Link

If your sub arrays contain only 1 items, you can use following code.

array_count_values(array_map('key', $array));

Here,

Ideone

###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,

Ideone

###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,

Ideone

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";
added 446 characters in body
Source Link
Shiplu Mokaddim
  • 57.5k
  • 20
  • 147
  • 193

If your sub arrays contain only 1 items, you can use following code.

array_count_values(array_map('key', $array));

Here,

Ideone

###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,

Ideone

If your sub arrays contain only 1 items, you can use following code.

array_count_values(array_map('key', $array));

Here,

Ideone

###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";
Source Link
Shiplu Mokaddim
  • 57.5k
  • 20
  • 147
  • 193

If your sub arrays contain only 1 items, you can use following code.

array_count_values(array_map('key', $array));

Here,

Ideone

lang-php

AltStyle によって変換されたページ (->オリジナル) /