Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Write a PHP program to find the transpose of a matrix (#5716)
Browse files Browse the repository at this point in the history
  • Loading branch information
BatinSimsek authored Jun 3, 2024
1 parent b876767 commit f1e0e03
Showing 1 changed file with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

function transpose($matrix) {
$result = [];
foreach ($matrix as $rowIndex => $row) {
foreach ($row as $colIndex => $value) {
$result[$colIndex][$rowIndex] = $value;
}
}
return $result;
}

$matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];

$transposedMatrix = transpose($matrix);
foreach ($transposedMatrix as $row) {
echo implode(' ', $row) . "\n";
}

0 comments on commit f1e0e03

Please sign in to comment.