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
Answer from ArtisticPhoenix on Stack OverflowI think you should not index by the prices, after all from a math point of view 5.0 and 5.00 does not make difference at all.
If you are obtaining values from a database you will get strigs everywhere. So you will have to cast (int)$key for the keys.
And in your foreach you are changing a temporary variable. $key exists only for the current iteration of the loop you will want to declare it as:
foreach($list_b_prices as &(int)
value){
if($price_count[$key] >= 2){
$key . "0";
}
}
Note the ampersand and the casting to integer. Although i'm not sure which will come first. But again: I think indexing by some different value shall give you a better result.
What about a nested loop ?
$all_prices = [$list_a_prices, $list_c_prices, $list_d_prices];
foreach($list_b_prices as
value){
foreach($all_prices as $array){
if(isset($array[$key])){
$list_b_prices[$key] .= '0';
}
}
}
Not elegant but it does the trick.
The issue is right here in the source data:
{
"code": null
},
Adding an array_filter() to the mix will resolve it:
$json = array_count_values(array_filter(array_column($json, "code")));
If you don't pass a callable to array_filter(), it will just filter out elements that are falsy, like null.
The error message should be read. Your data contains something that is not a string or integer.
$data = [
'ZA' => 16,
'VN' => 187,
'xx' => []
];
array_count_values($data);
Outputs the same error message as the array contains an element that is not a string or integer.
$array = array("Kyle","Ben","Sue","Phil","Ben","Mary","Sue","Ben");
$counts = array_count_values($array);
echo $counts['Ben'];
You can do this with array_keys and count.
$array = array("blue", "red", "green", "blue", "blue");
echo count(array_keys($array, "blue"));
Output:
3