I'm basically trying to perform a calculation with the first row and first column however I can't seem to get it working correctly.
function tableCreator($height_arrayy, $weight_arrayy) {
echo "<table><tr><th></th>";
foreach($height_arrayy as $value1) {
echo "<th>$value1</th>";
}
echo "</tr>";
foreach($weight_arrayy as $value2) {
echo "<tr>";
echo "<td>$value2</td>";
foreach($weight_arrayy as $value3) {
foreach($height_arrayy as $value4) {
echo "<td>" . ($value3) / ( pow( ($value4 / 100), 2 ) ) . "</td>";
}
}
echo "</tr>";
}
echo "</table>";
}
Result I am getting:
Desired Result:
you have added an un-necessary foreach loop, update your second foreach loop as below
foreach($weight_arrayy as $value2) {
echo "<tr>";
echo "<th>$value2</th>";
foreach($height_arrayy as $value4) {
echo "<td>" . ($value2) / ( pow( ($value4 / 100), 2 ) ) . "</td>";
}
echo "</tr>";
}
your code will return desired output. use round() if you want to round upto 2 or 3 decimal points.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.