PHP Program to find Matrix Transpose

Last Updated:

Find Matrix Transpose

In the following example, we will transpose the given matrix (two-dimensional array).

Example

PHP Compiler
<?php $arr = array( array(1, 1, 1), array(2, 2, 2), array(3, 3, 3), ); echo "Matrix (3 x 3): <br>"; for($i=0; $i<3; $i++) { for($j=0; $j<3; $j++) echo $arr[$i][$j] . " "; echo "<br>"; } for($i=0; $i<3; $i++) { for($j=$i+1; $j<3; $j++) { $n = $arr[$i][$j]; $arr[$i][$j] = $arr[$j][$i]; $arr[$j][$i] = $n; } } echo "<br>Matrix Transpose: <br>"; for($i=0; $i<3; $i++) { for($j=0; $j<3; $j++) echo $arr[$i][$j] ." "; echo "<br>"; } ?>

Share this Page

Meet the Author