Function to generate INI from Array

Asked

Viewed 342 times

4

There is a function that does the inverse of parse_ini_file to be able to generate a configuration file from an array?

2 answers

2

Apparently 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: https://stackoverflow.com/questions/17316873/php-array-to-a-ini-file

#Edit

Originally posted by: https://stackoverflow.com/users/2016221/rr in the above mentioned link.

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);

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

  • could transcribe the content of the link here?

  • Out of the code the only useful quote was about the comments.

2

There is no native and 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’s still beta.

However, one can easily find a code such as of this SOEN response:

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 of use:

$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.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.