Skip to content

Instantly share code, notes, and snippets.

@kalkulus
Created January 28, 2015 16:27
Show Gist options
  • Save kalkulus/e222bb5a4a899f72e13f to your computer and use it in GitHub Desktop.
Save kalkulus/e222bb5a4a899f72e13f to your computer and use it in GitHub Desktop.
Get YAML file in a flat structure with joined keys
use Symfony\Component\Yaml\Yaml;
function flattenKeys($data, $parent = ''){
$result = array();
if (!is_array($data)) {
return $result;
}
foreach ($data as $key => $value) {
$fullKey = ($parent == '') ? $key : $parent.'.'.$key;
if (is_array($value)){
$result = array_merge($result, flattenKeys($value, $fullKey));
} else {
$result[$fullKey] = $value;
}
}
return $result;
}
$yaml = Yaml::parse($pathToYmlFile);
$flatKeys = flattenKeys($yaml);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment