Matrix I/O in PHP 5.3

Inspired by the Grid Computing challenge at Code Golf, I came up with a couple of useful functions for reading and writing matrices in PHP 5.3

{[.matrix | 1.hilite(=php=)]}

Here is a demo page

{[.demo | 1.hilite(=php=)]}

whose result is

 

$input = ' A, B, C, D, E FFF, G, H, I, J K, L, MM, N, P Q, R, S, T, UUUU VVVVV, W, X, Y, Z ';
$output = ' A, B, C, D, E FFF, G, H, I, J K, L, MM, N, P Q, R, S, T, UUUU VVVVV, W, X, Y, Z ';
$output === $input

After uncommenting the transposition line, the result is

 

$input = ' A, B, C, D, E FFF, G, H, I, J K, L, MM, N, P Q, R, S, T, UUUU VVVVV, W, X, Y, Z ';
$output = ' A, FFF, K, Q, VVVVV B, G, L, R, W C, H, MM, S, X D, I, N, T, Y E, J, P, UUUU, Z ';
$output !== $input

2 Replies to “Matrix I/O in PHP 5.3”

  1. Parse error: syntax error, unexpected T_FUNCTION, expecting ‘)’

    function matrixToRowsByColumns($matrix, $glue = array(‘rows’ => “n”, ‘cols’ => ‘,’))
    {
    $result = array_map(function($row) use($glue)
    {
    return array_map(‘trim’, explode($glue[‘cols’], $row));
    }, explode($glue[‘rows’], trim($matrix))
    );
    return $result;
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.