Created
April 17, 2015 09:28
-
-
Save artoodetoo/f98dc2b46d9a74adbb83 to your computer and use it in GitHub Desktop.
Unlimited dimensions INI file
This file contains 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
<?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)); | |
} |
This file contains 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
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