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
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;
}
@ Edward
Did you see this is for PHP 5.3?