PHPで転置行列

<?php
$matrix = array(
        array(1, 2, 3),
        array(4, 5, 6),
        array(7, 8, 9),
        );

print_matrix($matrix);
print_matrix(transpose($matrix));

function transpose($matrix) {
    $transpose = null;

    foreach ($matrix as $row_k => $row) {
        foreach ($row as $col_k => $col) {
            $transpose[$col_k][$row_k] = $col;
        }
    }

    return $transpose;
}

function print_matrix($matrix) {
    echo "----- Print matrix -----\n";

    foreach ($matrix as $row) {
        foreach ($row as $col) {
            echo $col . ' ';
        }
        echo "\n";
    }
}