Whilst attempting to run the below code, my browser keeps responding with the below errors. How can I fix the below code, so these errors no longer present themselves?
To be clear, these errors appear only on the lines containing each of the below:
$high = $arr[$middleval+1];
$median = (($low+$high)/2);
Thanks
Code:
function median($arr)
{
sort($arr);
$count = count($arr); //count the number of values in array
$middleval = floor(($count-1)/2); // find the middle value, or the lowest middle value
if ($count % 2) { // odd number, middle is the median
$median = $arr[$middleval];
} else { // even number, calculate avg of 2 medians
$low = $matches[0];
$high = $arr[$middleval+1];
$median = (($low+$high)/2);
}
return $median;
}
Errors:
Notice: Undefined offset: 0 in medium.php on line 9
Notice: Undefined variable: matches in medium.php on line 10