Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I want to find mean, median and mode of an array. I can find mean and median but but when I run the program, I get

Warning: array_count_values(): Can only count STRING and INTEGER values! in C:\AppServ\www\tez\tez2.php on line 40" error fore finding mode.

I searched but couldn't fix it. Is there anyone who can help me with that ?

My Code:

<!DOCTYPE html>
    <title>Tez Deneme</title>
</head>
</body>
</html>
echo "Welcome to my project".'<br>'.'<br>'; 
$arr=array(1100,3150,4400,4400,5170,7450,7450,7450,8230 );
for($i=0; $i<=8; $i++)
    if ($arr[$i]<100) {
        $arr[$i]=$arr[$i];
    }else{
        $arr[$i]=$arr[$i]/1000;
        $arr1[$i]=$arr[$i];
function calculate($arr, $output){
    switch($output){
        case 'mean':
            $count = count($arr)+1;
            $sum = array_sum($arr);
            $total = $sum / $count;
        break;
        case 'median':
            rsort($arr);
            $middle = (count($arr) / 2)+1;
            $total = $arr[$middle-1];
        break;
        case 'mode':
            $v = array_count_values($arr); 
            arsort($v); 
            foreach($v as $k => $v){$total = $k; break;}
        break;
    return $total;
function sd_square($x, $total) { 
    return pow($x - $mean,2); 
function sd($arr) {
    return sqrt(array_sum(array_map("sd_square", $arr, array_fill(0,count($arr), (array_sum($arr) / count($arr)) ) ) ) / (count($arr)-1) );
if (isset($_POST['select'])) {
    someFunction();
echo '  '.'<br>';
echo "Values: ";
echo json_encode($arr).'<br>';
echo 'Mean: '.calculate($arr, 'mean').'<br>';
echo 'Median: '.calculate($arr, 'median').'<br>';
echo 'Mode: '.calculate($arr, 'mode').'<br>';
echo "Standart Derivation: ".sd($arr);

Hence the Can only count STRING and INTEGER values.

You can round it or do something else like cast it to a string etc...

$arr[$i]=$arr[$i]/1000;
$arr1[$i]=(string)$arr[$i];
function sd_square($x, $total) { return pow($x - $mean,2); }

The var $mean is undefined here.

Making those changes:

echo "Welcome to my project".'<br>'.'<br>'; $arr=array(1100,3150,4400,4400,5170,7450,7450,7450,8230 ); $arr1=[]; //<--- define this if all are < 100 its undefined for($i=0; $i<=8; $i++){ if ($arr[$i]<100) { //<-- clean up formatting. $arr[$i]=$arr[$i]; }else{ $arr[$i]=$arr[$i]/1000; $arr1[$i]=(string)$arr[$i]; //<-- cast to string function calculate($arr, $output){ switch($output){ case 'mean': $count = count($arr)+1; $sum = array_sum($arr); $total = $sum / $count; break; case 'median': rsort($arr); $middle = (count($arr) / 2)+1; $total = $arr[$middle-1]; break; case 'mode': $v = array_count_values($arr); arsort($v); foreach($v as $k => $v){$total = $k; break;} break; return $total; function sd_square($x, $total) { return pow($x - $total,2); } //<--changed to $total function sd($arr) { return sqrt(array_sum(array_map("sd_square", $arr, array_fill(0,count($arr), (array_sum($arr) / count($arr)) ) ) ) / (count($arr)-1) ); if (isset($_POST['select'])) { someFunction(); echo ' '.'<br>'; echo "Values: "; echo json_encode($arr).'<br>'; echo 'Mean: '.calculate($arr, 'mean').'<br>'; echo 'Median: '.calculate($arr, 'median').'<br>'; echo 'Mode: '.calculate($arr1, 'mode').'<br>'; echo "Standart Derivation: ".sd($arr);

Output

Welcome to my project
Values: [1.1,3.15,4.4,4.4,5.17,7.45,7.45,7.45,8.23]
Mean: 4.88
Median: 5.17
Mode: 7.45
Standart Derivation: 2.4035743059961

Sandbox

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.