Skip to content

Instantly share code, notes, and snippets.

@artoodetoo
Created April 17, 2015 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artoodetoo/f98dc2b46d9a74adbb83 to your computer and use it in GitHub Desktop.
Save artoodetoo/f98dc2b46d9a74adbb83 to your computer and use it in GitHub Desktop.
Unlimited dimensions INI file
<?php
/**
* Unroll dot-sequenced keys to new dimension
* @param array $source
* @return array
*/
function deep_load($source)
{
$result = [];
ksort($source);
foreach ($source as $key => $values) {
$x =& $result;
foreach (explode('.', $key) as $s) {
if (!array_key_exists($s, $x)) {
$x[$s] = [];
}
$x =& $x[$s];
}
$x = is_array($values) ? deep_load($values) : $values;
}
return $result;
}
/**
* Get values from .ini in more then two dimensions.
* Both sections and names can have dots to unroll.
* @param string $filename
* @return array
*/
function parse_ini_deep($filename)
{
return deep_load(parse_ini_file($filename, true));
}
omega = -1
sigma = 0
[first]
alfa = 1
[second]
beta.a = 2
beta.b = 3
[third.one]
gama = "ololo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment