Created
August 29, 2017 13:14
-
-
Save innermond/7ca32fec2c610065b8c05e01053dd49c to your computer and use it in GitHub Desktop.
array_vertical - create arrays comprised of same index position elements of original arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function array_vertical($source) { | |
| $keys = array_keys($source); | |
| $arr = array_values($source); | |
| $cut = array_map(null, ...$arr); | |
| foreach($cut as &$el) { | |
| $el = array_combine($keys, $el); | |
| $el = array_filter($el, function($v) {return ! is_null($v);}); | |
| } | |
| return $cut; | |
| $cut=[]; | |
| while (count($source)) { | |
| // definition collector | |
| $el = []; | |
| foreach($source as $k => &$v) { // &$v need to be reference for array_shift | |
| // no more definitions | |
| if (empty($v)) { | |
| // shorten life of while cycle here | |
| unset($source[$k]); | |
| continue; | |
| } | |
| // collect only first | |
| $el[$k] = array_shift($v); | |
| } | |
| if (! empty($el)) $cut[] = $el; | |
| } | |
| return $cut; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment