Skip to content

Instantly share code, notes, and snippets.

@Rudis1261
Created December 2, 2016 22:10
Show Gist options
  • Save Rudis1261/95ace50bdfccf9ae9f5fe3e46a841110 to your computer and use it in GitHub Desktop.
Save Rudis1261/95ace50bdfccf9ae9f5fe3e46a841110 to your computer and use it in GitHub Desktop.
Creating a recursive directory array
/**
* 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