I'm trying to implement a function for multiplying two numeric multidimensional arrays, I performed three cycles to iterate over the rows and columns of corresponding. However something is failing there.
The PHP is just not generating any web page, whenever I comment the function code and the function calling it fails...
function multiplicaMatriz($matrix1, $matrix2){
$ab = 0;
echo "<table border="1">";
for($i = 0; $i < 3; $i++) {
echo "<tr>";
for($j = 0; $j < 3; $j++) {
$ab = 0;
for($k = 0; $k < 3; $k++) {
$ab += (($matrix1[$i][$k])*($matrix2[$k][$j]));
}
echo "<td>".$ab."</td>";
}
echo "</tr>";
}
echo "</table>";
}
Rest of my code is here:
<html>
<head>
<title>
Novatos del PHP
</title>
</head>
<body> <center>
<?php
$m1 = array ( array (rand(0,100), rand(0,100), rand(0,100)),
array (rand(0,100), rand(0,100), rand(0,100)),
array (rand(0,100), rand(0,100), rand(0,100)),
//array (rand(0,100), rand(0,100), rand(0,100))
);
$m2 = array ( array (rand(0,100), rand(0,100), rand(0,100)),
array (rand(0,100), rand(0,100), rand(0,100)),
array (rand(0,100), rand(0,100), rand(0,100)),
//array (rand(0,100), rand(0,100), rand(0,100))
);
function imprimeMatriz($matrix){
echo "<table width=\"200\" border=\"1\">";
foreach($matrix as $row =>$rValue){
echo "<tr>";
foreach($rValue as $col =>$cValue){
echo "<td>".$cValue."</td>";
}
echo "</tr>";
}
echo "</table>";
}
function sumaMatriz($matrix1, $matrix2){
echo "<table width=\"200\" border=\"1\">";
foreach($matrix1 as $row1 =>$rValue1){
echo "<tr>";
foreach($rValue1 as $col1 =>$cValue1)
echo "<td>".($cValue1+$matrix2[$row1][$col1])."</td>";
echo "</tr>";
}
echo "</table>";
}
function multiplicaMatriz($matrix1, $matrix2){
$ab = 0;
echo "<table border="1">";
for($i = 0; $i < 3; $i++) {
echo "<tr>";
for($j = 0; $j < 3; $j++) {
$ab = 0;
for($k = 0; $k < 3; $k++) {
$ab += (($matrix1[$i][$k])*($matrix2[$k][$j]));
}
echo "<td>".$ab."</td>";
}
echo "</tr>";
}
echo "</table>";
}
print "<h2>Matriz 1</h2>";
imprimeMatriz($m1);
print "<br>";
print "<h2>Matriz 2</h2>";
imprimeMatriz($m2);
print "<br>";
print "<h2>Suma de matrices</h2>";
sumaMatriz($m1, $m2);
print "<br>";
print "<h2>Suma de matrices</h2>";
multiplicaMatriz($m1, $m2);
?>
</center> </body>
</html>