Skip to content

Instantly share code, notes, and snippets.

@vdubyna
Created February 22, 2017 11:59
Show Gist options
  • Save vdubyna/7b759e0b8d87f3ac1cdb3d27e74a1f50 to your computer and use it in GitHub Desktop.
Save vdubyna/7b759e0b8d87f3ac1cdb3d27e74a1f50 to your computer and use it in GitHub Desktop.
$k = [
'a/b/c/j',
'a/b/c',
'a/b/d/q',
'a/b/d',
'a/b/e',
'a/b/c/j',
'a/b/c',
'a/b/d/q',
'a/b/d',
'a/b/e',
'a/b',
'a',
'a/x/z',
'a/x/y',
'a/x',
];
$cats = array_map(function ($cat) {
$t = array_reverse(explode('/', $cat));
$i = array_shift($t);
return array_reduce($t, function ($carry, $item) {
return [$item => $carry];
}, [$i => []]);
}, $k);
print_r(array_merge_recursive(...$cats));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment