2 つの数値多次元配列を乗算する関数を実装しようとしています。対応する行と列を反復処理するために 3 つのサイクルを実行しました。しかし、そこで何かが失敗しています。
関数コードにコメントを付け、それを呼び出す関数が失敗するたびに、PHPはWebページを生成していません...
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>";
}
私のコードの残りはここにあります:
<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>