Created
January 28, 2015 16:27
-
-
Save kalkulus/e222bb5a4a899f72e13f to your computer and use it in GitHub Desktop.
Get YAML file in a flat structure with joined keys
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
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