PHP Operation INI Config file
Copy Code code as follows:
<?php
Write INI file
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 $key 2=> $elem 2)
{
if (Is_array ($elem 2))
{
For ($i =0 $i <count ($elem 2); $i + +)
{
$content. = $key 2. " [] = \ "". $elem 2[$i]. " \ n ";
}
}
else if ($elem 2== "") $content. = $key 2. "= \ n";
else $content. = $key 2. "= \" ". $elem 2." \ n ";
}
}
}
Else
{
foreach ($assoc _arr as $key => $elem)
{
if (Is_array ($elem))
{
for ($i =0; $i <count ($elem); $i + +)
{
$content. = $key 2. " [] = \ "". $elem [$i]. \ n ";
}
}
else if ($elem = = "") $content. = $key 2. "= \ n";
else $content. = $key 2. "= \" "$elem." \ n ";
}
}
if (! $handle = fopen ($path, ' W '))
{
return false;
}
if (!fwrite ($handle, $content))
{
return false;
}
Fclose ($handle);
return true;
}
Usage
//
$sampleData = Array (
' A ' => 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);
Read INI file
Public Function Readini ($name)
{
if (file_exists (sem_path. ' init/'. $name))
{
$data = Parse_ini_file (sem_path. ' init/'. $name, True);
if ($data)
{
return $data;
}
}
Else
{
return false;
}
}
If you are working in Python, you need to remove the double quotes on both sides of the configuration file value that PHP generates. Which is to generate the INI config file function above \"Get rid of that OK."