From 1f1e59bf55cbc31acb1b5bebfc45c78b53dc3372 Mon Sep 17 00:00:00 2001 From: ajant Date: Wed, 22 Feb 2017 11:41:41 +0100 Subject: [PATCH 1/2] License info fixed --- LICENSE.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE.md b/LICENSE.md index 766a0a5..dfda3fd 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) {{{year}}} {{{fullname}}} +Copyright (c) 2015-2017 Aleksandar Jovanovic Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 9de12963891eeaf45a064c3007d320c237579233 Mon Sep 17 00:00:00 2001 From: Petar Zivkovic Date: Tue, 2 Jan 2018 17:01:03 +0100 Subject: [PATCH 2/2] Add transpose method --- README.md | 8 +++++ src/Categories/Changers.php | 13 +++++++ test/SimpleArrayLibrary/TransposeTest.php | 41 +++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 test/SimpleArrayLibrary/TransposeTest.php diff --git a/README.md b/README.md index f25d890..7cd7ffb 100755 --- a/README.md +++ b/README.md @@ -287,4 +287,12 @@ exists SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1); // array(array('foo' => 1), array('foo' => 1)) SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1, false, false); // array(array('foo' => 2), array()) SimpleArrayLibrary::setColumn(array(array('foo' => 2), array()), 'foo', 1, true, false); // array(array('foo' => 2), array('foo' => 1)) +``` +transpose +------------------------------ +Transpose a matrix + +Turns rows into columns, and columns into rows. This is very handy when you need to reshape a dataset for a report or plot. +```php +SimpleArrayLibrary::transpose(array(array(1, 2, 3). array(4, 5, 6))); // array(array(1, 4), array(2, 5), array(3, 6)) ``` \ No newline at end of file diff --git a/src/Categories/Changers.php b/src/Categories/Changers.php index 998636c..de62f78 100644 --- a/src/Categories/Changers.php +++ b/src/Categories/Changers.php @@ -181,4 +181,17 @@ public static function setColumn(array $matrix, $column, $value, $insertIfMissin return $matrix; } + + /** + * @param array $matrix + * + * @return array + */ + public static function transpose(array $matrix) + { + // example #4 explains how this works + // http://php.net/manual/en/function.array-map.php + array_unshift($matrix, null); + return call_user_func_array('array_map', $matrix); + } } \ No newline at end of file diff --git a/test/SimpleArrayLibrary/TransposeTest.php b/test/SimpleArrayLibrary/TransposeTest.php new file mode 100644 index 0000000..0290c62 --- /dev/null +++ b/test/SimpleArrayLibrary/TransposeTest.php @@ -0,0 +1,41 @@ +assertEquals($data['expResult'], SimpleArrayLibrary::transpose($data['matrix'])); + } + + /** + * @return array + */ + public function getData() + { + return array( + // #0 column required & present + array( + array( + 'matrix' => array( + array(1, 2, 3), + array(4, 5, 6) + ), + 'expResult' => array( + array(1, 4), + array(2, 5), + array(3, 6) + ) + ) + ) + ); + } +} \ No newline at end of file