PHP添加到ini文件中的键

时间:2019-06-07 12:37:45

标签: php ini

我有一个ini文件,应该使用PHP Forms添加到其中。截至目前,我已有工作代码来创建一个不存在的部分,如果存在则替换一个值。我正在努力的事情是追加。

例如,这是我的ini文件。

[Persons]
names = "travis", "jackson"
color = "blue"

在PHP方面,我有一个表单来插入节,键和值

假设输入部分是Person,键是color,值是red

我想要的新ini文件应该是

[Persons]
names = "travis", "jackson"
color = "blue", "red"

我尝试修改的原始代码:(taken from this stack article

function config_set($config_file, $section, $key, $value) {
    $config_data = parse_ini_file($config_file, true);
    $config_data[$section][$key] = $value;
    $new_content = '';
    foreach ($config_data as $section => $section_content) {
        $section_content = array_map(function($value, $key) {
            return "$key=$value";
        }, array_values($section_content), array_keys($section_content));
        $section_content = implode("\n", $section_content);
        $new_content .= "[$section]\n$section_content\n";
    }
    file_put_contents($config_file, $new_content);
}

我的想法只是将其附加到当前密钥上,但是我不确定该怎么做

编辑:尝试过的事情

这使修改前的ini文件零更改

function config_set($config_file, $section, $key, $value) {
    $config_data = parse_ini_file($config_file, true);
    $config_data[$section][$key] = $value;
    $new_content = "[$section]\n$value";
    foreach ($config_data as $section => $section_content) {
        $section_content = array_map(function($value, $key) {
            return "$key=$value";
        }, array_values($section_content), array_keys($section_content));
        $section_content = implode("\n", $section_content);
        $new_content .= "\n$section_content\n";
    }
    file_put_contents($config_file, $new_content);
 }

这打破了页面

function config_set($config_file, $section, $key, $value) {
    $config_data = parse_ini_file($config_file, true);
    $config_data[$section][$key] = $old_val;
    $new_content = '';
    foreach ($config_data as $section => $section_content) {
        $section_content = array_map(function($value, $key) {
            return "$key=$old_val, $value";
        }, array_values($section_content), array_keys($section_content));
        $section_content = implode("\n", $section_content);
        $new_content .= "[$section]\n$section_content\n";
    }
    file_put_contents($config_file, $new_content);
 }

1 个答案:

答案 0 :(得分:1)

您可以添加此条件以检查“颜色”(关键部分)内是否存在值,并相应地附加新值:

if (empty($config_data[$section][$key])) {
    $config_data[$section][$key] = $value;
} else {
    $config_data[$section][$key] .= ',' . $value;
}   

完整代码:

function config_set($config_file, $section, $key, $value) {
    $config_data = parse_ini_file($config_file, true);
    if (empty($config_data[$section][$key])) {
        $config_data[$section][$key] = $value;
    } else {
        $config_data[$section][$key] .= ',' . $value;
    }    
    $new_content = '';
    foreach ($config_data as $section => $section_content) {
        $section_content = array_map(function($value, $key) {
            return "$key=$value";
        }, array_values($section_content), array_keys($section_content));
        $section_content = implode("\n", $section_content);
        $new_content .= "[$section]\n$section_content\n";
    }
    file_put_contents($config_file, $new_content);
}