Example:
$a[] = '56'; $a[] = '66'; $a[] = ''; $a[] = '58'; $a[] = '85'; $a[] = ''; $a[] = ''; $a[] = '76'; $a[] = ''; $a[] = '57';
Actually how to find average value from this array excluding empty. please help to resolve this problem.
Simple approach to finding the average of an array We would first count the total number of elements in an array followed by calculating the sum of these elements and then dividing the obtained sum by the total number of values to get the Average / Arithmetic mean.
$avg = $total/$rows; echo $avg; As you are using string variables in numeric calculations, PHP is converting it wrongly. The 1,806 is taken as 1.
Average This is the arithmetic mean, and is calculated by adding a group of numbers and then dividing by the count of those numbers. For example, the average of 2, 3, 3, 5, 7, and 10 is 30 divided by 6, which is 5.
first you need to remove empty values, otherwise average will be not accurate.
so
$a = array_filter($a); $average = array_sum($a)/count($a); echo $average;
DEMO
More concise and recommended way
$a = array_filter($a); if(count($a)) { echo $average = array_sum($a)/count($a); }
See here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With