$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
-
I assume there is actually an array containing these associative arrays?Alvin Wong– Alvin Wong2012年12月25日 08:45:44 +00:00Commented Dec 25, 2012 at 8:45
-
are you talking about 2-d array?Rukmi Patel– Rukmi Patel2012年12月25日 08:47:24 +00:00Commented Dec 25, 2012 at 8:47
-
@AlvinWong it is the result that loop in the array. hard to say it in English. :(Najiullah.com– Najiullah.com2012年12月25日 09:01:20 +00:00Commented Dec 25, 2012 at 9:01
-
@Najiullah.com then please show is in code ;)thescientist– thescientist2012年12月25日 09:10:10 +00:00Commented Dec 25, 2012 at 9:10
3 Answers 3
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";
answered Dec 25, 2012 at 9:04
Shiplu Mokaddim
57.5k20 gold badges147 silver badges193 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
Najiullah.com
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?
Najiullah.com
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));
Najiullah.com
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.. :(
Najiullah.com
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. ^_^
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
davidethell
12.1k6 gold badges47 silver badges67 bronze badges
2 Comments
Waleed Khan
Or use
array_count_values.davidethell
@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.
$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
Passerby
10.1k2 gold badges37 silver badges53 bronze badges
5 Comments
Najiullah.com
thanks @passerby i will try it... :) but how to echo the result?
Passerby
@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.Najiullah.com
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.
Passerby
@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.
Najiullah.com
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. :)
lang-php