问题描述:
tp5,extra配置里面的参数需要修改值。获取配置文件然后合并,再重新把要写入的数组写入文件。
解决办法:
一维数组:
public function setConfigv(){
$path = './application/extra/api.php';
$file = (include $path);
$config = array(
'paytype' => 3
);
$res = array_merge($file, $config);
$str = '<?php'.PHP_EOL .' return ['.PHP_EOL;
foreach ($res as $key => $value) {
$str .= ' \'' . $key . '\'' . '=>' . '\'' . $value . '\'' . ','.PHP_EOL;
}
$str .= ']; ';
if (file_put_contents($path, $str)) {
$this->success('添加成功');
} else {
$this->error('添加失败');
}
}
多维数组:
public function setConfigm($path,$data){
$file=include $path;
$res = array_merge($file, $data);
$str = '<?php '.PHP_EOL. 'return [ '.PHP_EOL;
foreach ($res as $key => $value){
// '\'' 单引号转义
//$str .= '\''.$key.'\''.'=>'.'\''.$value.'\''.',';
if (is_array($value)){
$str.= ' \''.$key.'\''.'=>['.PHP_EOL;
foreach ($value as $k=>$v){
$str.=' \''.$k.'\''.'=>'.'\''.$v.'\''.','.PHP_EOL;
}
$str.=' ],'.PHP_EOL;
}else{
$str.='\''.$key.'\''.'=>'.'\''.$value.'\''.','.PHP_EOL;
}
};
$str .= ']; '.PHP_EOL;
$upfile =file_put_contents($path, $str);//写入文件
}