Created
March 29, 2017 20:10
-
-
Save anth-3/3c0273fee80615d5ce54969d7ba4ea59 to your computer and use it in GitHub Desktop.
One-Dimensional Array to Nested Multi-Dimensional Array
This file contains 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
<?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