PHP Online Compiler
Example: Multiplication of Two Matrix in PHP of Same Dimensions using For loop
C
C++
C#
Java
Python
PHP
main.php
STDIN
Run
<?php // Multiplication of Two Matrix in PHP of Same Dimensions using For loop $x = array ( array (32, 54, 65), array (45, 54, 65), array (45, 32, 65), array (45, 32, 54) ); $y = array ( array (54, 24, 76), array (90, 24, 76), array (90, 54, 76), array (90, 54, 24) ); // $x & $y - To store the two matrix elements of 4 x 3 dimensions $m = array (); // $m - To store multiplication of matrices echo "-----The multiplication of the matrices is-----\n\n"; // It's the calculation of matrices's multiplications for ($i = 0; $i < count ($x); $i++) { $m[] = array (); for ($j = 0; $j < count ($x[$i]); $j++) { $m[$i][] = 0; for ($k = 0; $k < count ($x[$i]); $k++) { $m[$i][$j] += $x[$i][$k] * $y[$k][$j]; } } } // This will display matrices' outputs for ($i = 0; $i < count ($x); $i++) { echo "\t"; for ($j = 0; $j < count ($x[$i]); $j++) { echo $m[$i][$j]."\t"; } echo "\n"; } ?>
Output
Clear
ADVERTISEMENTS