PHP 添加到 ini 文件中的键
PHP Add to key in ini file
我有一个 ini 文件,我应该将其添加到 PHP 表单中。截至目前,我有工作代码来创建一个部分(如果它不存在),并替换一个值(如果它存在)。我正在努力解决的问题是追加。
例如,这是我现在的 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);
}
您可以添加此条件以检查 "color"(关键部分)中是否有值并相应地附加新值:
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);
}
我有一个 ini 文件,我应该将其添加到 PHP 表单中。截至目前,我有工作代码来创建一个部分(如果它不存在),并替换一个值(如果它存在)。我正在努力解决的问题是追加。
例如,这是我现在的 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);
}
您可以添加此条件以检查 "color"(关键部分)中是否有值并相应地附加新值:
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);
}