What are some best practices for efficiently transposing a two-dimensional array in PHP?

Transposing a two-dimensional array in PHP involves swapping the rows and columns of the array. One efficient way to transpose a two-dimensional array is to use nested loops to iterate over the rows and columns, swapping the elements accordingly.

function transposeArray($arr) {
    $transposedArray = [];
    foreach ($arr as $rowKey => $row) {
        foreach ($row as $colKey => $value) {
            $transposedArray[$colKey][$rowKey] = $value;
        }
    }
    return $transposedArray;
}

// Example usage
$array = [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
];

$transposedArray = transposeArray($array);
print_r($transposedArray);