0

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>
<html>
<head>
    <title>Tez Deneme</title>
</head>
<body>
</body>
</html>

<?php
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);
?>
2
  • Well, dump whatever you are passing to that function and ensure it is a string or int. Commented Mar 7, 2019 at 16:24
  • You have decimals in your array, change it to string Commented Mar 7, 2019 at 16:31

1 Answer 1

1

In your code

$arr[$i]=$arr[$i]/1000;
$arr1[$i]=$arr[$i];

Your $arr1 is a collection of floats:

Array
(
    [0] => 1.1
    [1] => 3.15
    [2] => 4.4
    [3] => 4.4
    [4] => 5.17
    [5] => 7.45
    [6] => 7.45
    [7] => 7.45
    [8] => 8.23
)

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];

Also

function sd_square($x, $total) { return pow($x - $mean,2); }

The var $mean is undefined here.

Making those changes:

<?php

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

Sign up to request clarification or add additional context in comments.

4 Comments

It has to be decimal, can't I find mode of decimal array ? @ArtisticPhoenix ?
if it's technically a string you can.
You are my real hero! Thank you so much @ArtisticPhoenix
It was easy, no problem.

Your Answer

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.