I'm starting in the PHP I'm trying to divide 2 arrays and get the result in a third array, but I get Fatal error: Uncaught DivisionByZeroError. Thank you very much, the code is:
<?php
$visitas = array(4, 5, 8, 9, 10);
$ventas = array(2, 4, 5, 6, 1);
if (($visitas > 0) && ($ventas > 0)) {
for ($i = 1; $i <= count($visitas); $i++) {
$division[] = $visitas[$i] * $ventas[$i];
}
var_dump($division);
}
?>
There are several flaws, apart from the fact that you do not divide but multiply.
The conditional is weird, because you evaluate the array variables as if they were numbers comparing them with
0
, when I would do it withcount
both variables to know if they have elements or not.Then you don't check if both variables have the same number of elements, and the
$i=1
makes you skip the first one, it should be$i=0
, and this: it$i <= count($visitas)
should be this:$i <= count($visitas)-1
because the number of elements of an array with their indices is not the same, that start at0
, not1
, so you have to subtract1
from their count in the conditional.All this that I have said would be like this:
resulting in this (I show it with print_r which I like better):
And here I show it with var_dump so you can see the type of results of each element:
Let us know if it worked for you.