Function to generate INI from Array

4

Is there a function that does the inverse of parse_ini_file in order to generate a configuration file from an array?

    
asked by anonymous 31.03.2014 / 19:51

2 answers

2

There is no native, stable function (at least as far as I know). When I needed to do this, I did it manually.

There is a PEAR extension called Config_Lite , but it is still beta.

However, you can easily find a code such as from this SOEN answer :

function write_ini_file($assoc_arr, $path, $has_sections=FALSE) { 
    $content = ""; 
    if ($has_sections) { 
        foreach ($assoc_arr as $key=>$elem) { 
            $content .= "[".$key."]\n"; 
            foreach ($elem as $key2=>$elem2) { 
                if(is_array($elem2)) 
                { 
                    for($i=0;$i<count($elem2);$i++) 
                    { 
                        $content .= $key2."[] = \"".$elem2[$i]."\"\n"; 
                    } 
                } 
                else if($elem2=="") $content .= $key2." = \n"; 
                else $content .= $key2." = \"".$elem2."\"\n"; 
            } 
        } 
    } 
    else { 
        foreach ($assoc_arr as $key=>$elem) { 
            if(is_array($elem)) 
            { 
                for($i=0;$i<count($elem);$i++) 
                { 
                    $content .= $key2."[] = \"".$elem[$i]."\"\n"; 
                } 
            } 
            else if($elem=="") $content .= $key2." = \n"; 
            else $content .= $key2." = \"".$elem."\"\n"; 
        } 
    } 

    if (!$handle = fopen($path, 'w')) { 
        return false; 
    } 
    if (!fwrite($handle, $content)) { 
        return false; 
    } 
    fclose($handle); 
    return true; 
}

Example usage:

$sampleData = array(
                'first' => array(
                    'first-1' => 1,
                    'first-2' => 2,
                    'first-3' => 3,
                    'first-4' => 4,
                    'first-5' => 5,
                ),
                'second' => array(
                    'second-1' => 1,
                    'second-2' => 2,
                    'second-3' => 3,
                    'second-4' => 4,
                    'second-5' => 5,
                ));
write_ini_file($sampleData, './data.ini', true);

On the other hand, my personal suggestion is to use Json to store data. In addition to being more flexible (hierarchy, data types), there are already native functions for read and create from any String.

    
31.03.2014 / 20:02
2

It seems that PHP does not have a native function for this, you have to create your own function.

Take a look at the first answer to this international OS question: link

#edit

Originally Posted by: link in the link quoted above.

function arr2ini(array $a, array $parent = array())
{
    $out = '';
    foreach ($a as $k => $v)
    {
        if (is_array($v))
        {
            //subsection case
            //merge all the sections into one array...
            $sec = array_merge((array) $parent, (array) $k);
            //add section information to the output
            $out .= '[' . join('.', $sec) . ']' . PHP_EOL;
            //recursively traverse deeper
            $out .= arr2ini($v, $sec);
        }
        else
        {
            //plain key->value case
            $out .= "$k=$v" . PHP_EOL;
        }
    }
    return $out;
}

Example of using the function below with a multi-dimensional array:

$x = array(
  'section1' => array(
    'key1' => 'value1',
    'key2' => 'value2',
    'subsection' => array(
      'subkey' => 'subvalue',
      'further' => array('a' => 5),
      'further2' => array('b' => -5))));
echo arr2ini($x);

It is also worth remembering that there is no way to preserve the comments that were in the INI file, it would be necessary to create a more complex parse function for this.

    
31.03.2014 / 20:00