Created
December 2, 2016 22:10
-
-
Save Rudis1261/95ace50bdfccf9ae9f5fe3e46a841110 to your computer and use it in GitHub Desktop.
Creating a recursive directory array
This file contains hidden or 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
/** | |
* Sets a value in a nested array based on path | |
* See http://stackoverflow.com/a/9628276/419887 | |
* | |
* @param array $array The array to modify | |
* @param string $path The path in the array | |
* @param mixed $value The value to set | |
* @param string $delimiter The separator for the path | |
* @return The previous value | |
*/ | |
function set_nested_array_value(&$array, $path, &$value, $delimiter = '/') { | |
$pathParts = explode($delimiter, $path); | |
$current = &$array; | |
foreach($pathParts as $key) { | |
$current = &$current[$key]; | |
} | |
$backup = $current; | |
$current = $value; | |
return $backup; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment