Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not sure if I should edit my old answer or add a new one.</p> <p>Try this version of it, should be what you're looking for.</p> <pre><code>function parse_ini_advanced($array) { $returnArray = array(); if (is_array($array)) { foreach ($array as $key =&gt; $value) { $e = explode(':', $key); if (!empty($e[1])) { $x = array(); foreach ($e as $tk =&gt; $tv) { $x[$tk] = trim($tv); } $x = array_reverse($x, true); foreach ($x as $k =&gt; $v) { $c = $x[0]; if (empty($returnArray[$c])) { $returnArray[$c] = array(); } if (isset($returnArray[$x[1]])) { $returnArray[$c] = array_merge($returnArray[$c], $returnArray[$x[1]]); } if ($k === 0) { $returnArray[$c] = array_merge($returnArray[$c], $array[$key]); } } } else { $returnArray[$key] = $array[$key]; } } } return $returnArray; } function recursive_parse($array) { $returnArray = array(); if (is_array($array)) { foreach ($array as $key =&gt; $value) { if (is_array($value)) { $array[$key] = recursive_parse($value); } $x = explode('.', $key); if (!empty($x[1])) { $x = array_reverse($x, true); if (isset($returnArray[$key])) { unset($returnArray[$key]); } if (!isset($returnArray[$x[0]])) { $returnArray[$x[0]] = array(); } $first = true; foreach ($x as $k =&gt; $v) { if ($first === true) { $b = $array[$key]; $first = false; } $b = array($v =&gt; $b); } $returnArray[$x[0]] = array_merge_recursive($returnArray[$x[0]], $b[$x[0]]); } else { $returnArray[$key] = $array[$key]; } } } return $returnArray; } </code></pre> <p>Would be called like this:</p> <pre><code>$array = parse_ini_file('test.ini', true); $array = recursive_parse(parse_ini_advanced($array)); </code></pre> <p>This could be done a lot better/clearer but for a simple solution it should work just fine.</p> <p>If your config is:</p> <pre><code>[foo] a = 1 b.a = 2 b.b = 3 b.c = 4 c = 5 [bar : foo] b.x.c = 33 b.b = 17 c = 42 [hot : bar : foo] b.a = 83 b.d = 23 </code></pre> <p>The output should be:</p> <pre><code>Array ( [foo] =&gt; Array ( [a] =&gt; 1 [b] =&gt; Array ( [a] =&gt; 2 [b] =&gt; 3 [c] =&gt; 4 ) [c] =&gt; 5 ) [bar] =&gt; Array ( [a] =&gt; 1 [b] =&gt; Array ( [a] =&gt; 2 [b] =&gt; 17 [c] =&gt; 4 [x] =&gt; Array ( [c] =&gt; 33 ) ) [c] =&gt; 42 ) [hot] =&gt; Array ( [a] =&gt; 1 [b] =&gt; Array ( [a] =&gt; 83 [b] =&gt; 17 [c] =&gt; 4 [x] =&gt; Array ( [c] =&gt; 33 ) [d] =&gt; 23 ) [c] =&gt; 42 ) ) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload