0
$nialiakhirpraktikum = $ntugasakhir+$ratarata; //from other process 
if ($nialiakhirpraktikum>79) { $grade="A"; } 
else if ($nialiakhirpraktikum<=79 AND $nialiakhirpraktikum>67) { $grade="B"; } 
else if ($nialiakhirpraktikum<=67 AND $nialiakhirpraktikum>55) { $grade="C"; } 
else if ($nialiakhirpraktikum<=55 AND $nialiakhirpraktikum>44) { $grade="D"; } 
else { $grade="E"; } 
$array = array($grade); 
print_r(array_count_values($array));

I have some result in array like this:

Array ( [B] => 1 ) 
Array ( [B] => 1 ) 
Array ( [C] => 1 ) 
Array ( [C] => 1 ) 
Array ( [B] => 1 ) 
Array ( [B] => 1 ) 
Array ( [B] => 1 ) 
Array ( [B] => 1 ) 
Array ( [B] => 1 ) 
Array ( [B] => 1 )

how to get result as following:

score for B = 8
score for C = 2
Cœur
39k25 gold badges207 silver badges282 bronze badges
asked Dec 25, 2012 at 8:43
4
  • I assume there is actually an array containing these associative arrays? Commented Dec 25, 2012 at 8:45
  • are you talking about 2-d array? Commented Dec 25, 2012 at 8:47
  • @AlvinWong it is the result that loop in the array. hard to say it in English. :( Commented Dec 25, 2012 at 9:01
  • @Najiullah.com then please show is in code ;) Commented Dec 25, 2012 at 9:10

3 Answers 3

2

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";
answered Dec 25, 2012 at 9:04
Sign up to request clarification or add additional context in comments.

4 Comments

my code for : "array('B' => '1'), array('B'=>'1'), array('B' => '1'), array("C"=>"1"), array('C'=>'1')" is from looping. code in the first question is final result. how to replace that?
My php code: $nialiakhirpraktikum = $ntugasakhir+$ratarata; //from other process if ($nialiakhirpraktikum>79) { $grade="A"; } else if ($nialiakhirpraktikum<=79 AND $nialiakhirpraktikum>67) { $grade="B"; } else if ($nialiakhirpraktikum<=67 AND $nialiakhirpraktikum>55) { $grade="C"; } else if ($nialiakhirpraktikum<=55 AND $nialiakhirpraktikum>44) { $grade="D"; } else { $grade="E"; } $array = array($grade); print_r(array_count_values($array));
thanks for your edit and your code. but my result just show like this: score for B = 1 whats wrong? i follow your instruction and fail.. :(
thanks for your help. my problem is solved... like your instruction. i just wrong put $array = array(); before loop start. :p thanks for your help. ^_^
0

Here is a function that will return the sum for a particular letter:

function getTotal($key, $array) {
 $total = 0;
 foreach ($array as $currentArray) {
 foreach ($currentArray as $currentKey => $currentValue) {
 if ($key === $currentKey) {
 $total += $currentValue;
 }
 }
 }
 return $total;
}

Then use it like:

$totalForB = getTotal('B', $myArray);
answered Dec 25, 2012 at 8:50

2 Comments

@WaleedKhan, that will work if all values are 1, but I was inferring from his example that he wanted to count the actual value of a letter, not just the number of occurrences.
0
$base=array(array("B"=>1),array("C"=>1),array("B"=>1),array("B"=>1),array("C"=>1));
print_r($base); // just to debug
$score=array_reduce($base,function(&$rst,$i){
 foreach($i as $k=>$s){
 if(empty($rst[$k])){
 $rst[$k]=0;
 }
 $rst[$k]+=$s;
 }
 return $rst;
},array());
print_r($score);

output of print_r($base):

Array
(
 [0] => Array
 (
 [B] => 1
 )
 [1] => Array
 (
 [C] => 1
 )
 [2] => Array
 (
 [B] => 1
 )
 [3] => Array
 (
 [B] => 1
 )
 [4] => Array
 (
 [C] => 1
 )
)

output of print_r($score):

Array
(
 [B] => 3
 [C] => 2
)
answered Dec 25, 2012 at 9:02

5 Comments

thanks @passerby i will try it... :) but how to echo the result?
@Najiullah.com I'm not sure if I understand you...$score contains scores for each key (as I've print_r in the last line). I'll update the answer to make it more clear.
my code for : $base=array(your array in the example on top); how to replace "your array in the example on top" using my code? because my code is a result from looping.
@Najiullah.com Try to demonstrate how are you getting the result in your question so that we can help accordingly -- we all are now assuming you have already had that array at hand.
a very,very,very,very quick response from all members. I am confused as to which one to answer first. :( ok everyone, I will try all the code that you provide. I say many thanks to you all. :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.