Skip to content

Instantly share code, notes, and snippets.

@innermond
Created August 29, 2017 13:14
Show Gist options
  • Save innermond/7ca32fec2c610065b8c05e01053dd49c to your computer and use it in GitHub Desktop.
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
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