Skip to content

Instantly share code, notes, and snippets.

@anth-3
Created March 29, 2017 20:10
Show Gist options
  • Save anth-3/3c0273fee80615d5ce54969d7ba4ea59 to your computer and use it in GitHub Desktop.
Save anth-3/3c0273fee80615d5ce54969d7ba4ea59 to your computer and use it in GitHub Desktop.
One-Dimensional Array to Nested Multi-Dimensional Array
<?php
$array = array(1, 2, 3);
var_dump($array);
function array2NestedArray($array)
{
if (is_array($array) && count($array) > 1) {
return (array(array_shift(array_values($array)) => array2NestedArray(array_slice($array, 1))));
} else {
return (array(array_shift(array_values($array)) => []));
}
}
$nested = array2NestedArray($array);
var_dump($nested);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment