Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 918441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:22:31+00:00 2026-05-15T18:22:31+00:00

Basically, I would like a simple, easy, one-file way to parse an INI file

  • 0

Basically, I would like a simple, easy, one-file way to parse an INI file with “advanced” features, like section inheritance and property nesting, like Zend_Config_Ini.

For example:

[foo]
a = 1
b.a = 2
b.b = 3
b.c = 4
c = 5

[bar : foo]
b.b = 17
c = 42

Would parse into

array(
  'foo'=>array(
    'a'=>'1',
    'b'=>array(
      'a'=>'2',
      'b'=>'3',
      'c'=>'4'
    ),
    'c'=>'5'
  ),
  'bar'=>array(
    'a'=>'1',
    'b'=>array(
      'a'=>'2',
      'b'=>'17',
      'c'=>'4'
    ),
    'c'=>'42'
  )
)

PHP’s built-in parse_ini_file, doesn’t handle anything other than simple INI’s with simple sections and simple keys.

My problem with using Zend_Config_Ini is that I would have to include virtually the whole Zend_Config subpackage, and is super-bloated and configurable.

Is there a small and simple library available to parse this?
If not, is there an easy implementation I’m not seeing?

By small and simple, I mean something like the sfYaml of INI files.

To my (very inexperienced) eyes, I would have to parse through once with parse_ini_file, then come back and resolve inheritance, then run through each section and expand the keys recursively…

UPDATE: Since this seems to be a popular question, I would like to note that I have a simple class implementing this on GitHub, feel free to send pull requests, issues, etc.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T18:22:32+00:00Added an answer on May 15, 2026 at 6:22 pm

    Not sure if I should edit my old answer or add a new one.

    Try this version of it, should be what you’re looking for.

    function parse_ini_advanced($array) {
        $returnArray = array();
        if (is_array($array)) {
            foreach ($array as $key => $value) {
                $e = explode(':', $key);
                if (!empty($e[1])) {
                    $x = array();
                    foreach ($e as $tk => $tv) {
                        $x[$tk] = trim($tv);
                    }
                    $x = array_reverse($x, true);
                    foreach ($x as $k => $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 => $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 => $v) {
                        if ($first === true) {
                            $b = $array[$key];
                            $first = false;
                        }
                        $b = array($v => $b);
                    }
                    $returnArray[$x[0]] = array_merge_recursive($returnArray[$x[0]], $b[$x[0]]);
                } else {
                    $returnArray[$key] = $array[$key];
                }
            }
        }
        return $returnArray;
    }
    

    Would be called like this:

    $array = parse_ini_file('test.ini', true);
    $array = recursive_parse(parse_ini_advanced($array));
    

    This could be done a lot better/clearer but for a simple solution it should work just fine.

    If your config is:

    [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
    

    The output should be:

    Array
    (
    [foo] => Array
        (
            [a] => 1
            [b] => Array
                (
                    [a] => 2
                    [b] => 3
                    [c] => 4
                )
    
            [c] => 5
        )
    
    [bar] => Array
        (
            [a] => 1
            [b] => Array
                (
                    [a] => 2
                    [b] => 17
                    [c] => 4
                    [x] => Array
                        (
                            [c] => 33
                        )
    
                )
    
            [c] => 42
        )
    
    [hot] => Array
        (
            [a] => 1
            [b] => Array
                (
                    [a] => 83
                    [b] => 17
                    [c] => 4
                    [x] => Array
                        (
                            [c] => 33
                        )
    
                    [d] => 23
                )
    
            [c] => 42
        )
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.