<?php
$array = array(6,8,9,8,9,8,5,4,4,9);
function Counting($array){
$freq_data = array_count_values($array);
$unique_elements = array_map('intval',array_keys($freq_data));
$min_element = min($unique_elements);
$max_element = max($unique_elements);
$percentage_data = [];
$total = count($array);
for($i = $min_element; $i <= $max_element ; ++$i){
$current_percentage = isset($freq_data[$i]) ? ($freq_data[$i] / $total) : 0;
$percentage_data[$i] = $i > $min_element ? $percentage_data[$i-1] + $current_percentage : $current_percentage;
}
uksort($percentage_data,function($a,$b){
return strnatcmp($a,$b);
});// just to be sure that associative hash keys are in ascending order
return $percentage_data;
}
print_r(Counting($array));
- Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.26, 8.4.1 - 8.4.13
- Array
(
[4] => 0.2
[5] => 0.3
[6] => 0.4
[7] => 0.4
[8] => 0.7
[9] => 1
)
preferences:
107.73 ms | 407 KiB | 5 Q