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?