Skip to content

Instantly share code, notes, and snippets.

@zmf
Created December 26, 2019 14:54
Show Gist options
  • Save zmf/11eeae4ee02030ad4b406b922ca4db6a to your computer and use it in GitHub Desktop.
Save zmf/11eeae4ee02030ad4b406b922ca4db6a to your computer and use it in GitHub Desktop.
vertical_to_horizontal_array.php
<?php
$flat = [ 'this', 'seems', 'a', 'bit', 'strange'];
$nested = [];
function v2h_array(&$flat, &$nested) {
$nested[$flat[0]] = [];
while (count($flat) > 1) {
v2h_array(array_splice($flat, 1, count($flat)-1), $nested[$flat[0]]);
}
return false;
}
v2h_array($flat, $nested);
var_dump($nested);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment