How to find the average by the number of filled vectors?

1

I need to calculate the average of a vector, but considering only those that had the fields filled out.

For example:

HTML:

<label for="Cmes222">Valor:</label>
<input type="text" id="Cmes222" name="Tmes222"><br>
<label for="Cmes444">Valor:</label>
<input type="text" id="Cmes444" name="Tmes444"><br>
<label for="Cmes666">Valor:</label>
<input type="text" id="Cmes666" name="Tmes666"><br>
<label for="Cmes888">Valor:</label>
<input type="text" id="Cmes888" name="Tmes888"><br>

PHP:

$salbasetar[0] = $_POST ["Tmes222"]; 
$salbasetar[1] = $_POST ["Tmes444"];
$salbasetar[2] = $_POST ["Tmes666"];
$salbasetar[3] = $_POST ["Tmes888"];

$salbasetarm = ($salbasetar[0] + $salbasetar[1] + $salbasetar[2] + $salbasetar[3]) / 4;

So instead of 4, I want to divide by the number of vectors that have been filled. I've done a lot of research but nothing has worked. I think I should be able to do with count or foreach , but I could not do ... could anyone give an example?

    
asked by anonymous 18.04.2015 / 18:16

1 answer

1

I found the answer in this question from SOen .

Basically, what worked was:

$result = count(array_filter($salbasetar));

$salbasetarm = ($salbasetar[0] + $salbasetar[1] + $salbasetar[2] + $salbasetar[3]) / $result;
    
18.04.2015 / 19:37