一、读取配置文件
config.php
------------------
<?php
return array(
'a' => 1,
'b' => 2
);
?>
-------------------
getconfig.php
---------------------------
<?php
$config = require('config.php');
print_r($config);
?>
---------------------
$config 是一个数组变量,这就是 return 函数的妙用
config.php
--------------------------
<?php
return array(
'master'=>array(
'master_host'=>'1',
'master_name'=>'1',
'master_password'=>'1',
'master_port'=>'1',
'slave1'=>array(
'slave_host'=>'2',
'slave_name'=>'2',
'slave_password'=>'2',
'slave_port'=>'2',
),
'slave2'=>array(
'slave_host'=>'3',
'slave_name'=>'3',
'slave_password'=>'3',
'slave_port'=>'3',
),
),
'master1'=>array(
'master_host'=>'5',
'master_name'=>'5',
'master_password'=>'5',
'master_port'=>'5',
'slave1'=>array(
'slave_host'=>'6',
'slave_name'=>'6',
'slave_password'=>'6',
'slave_port'=>'6',
),
'slave2'=>array(
'slave_host'=>'7',
'slave_name'=>'7',
'slave_password'=>'7',
'slave_port'=>'7',
),
),
);
getconfig.php
--------------------------
<?php
$config = require('config.php');
$config['db']['master']['host']= '';
$config['db']['master']['name']= '';
$config['db']['master']['password']= '';
$config['db']['master']['port']= '';
$config['db']['master']['slave1']['host']= '';
$config['db']['master']['slave1']['name']= '';
$config['db']['master']['slave1']['password']= '';
$config['db']['master']['slave1']['port']= '';
$config['db']['master']['slave2']['host']= '';
$config['db']['master']['slave2']['name']= '';
$config['db']['master']['slave2']['password']= '';
$config['db']['master']['slave2']['port']= '';
{
二、更新配置文件
$path='路径';//写入路径
$file=include $path;
$res = array_merge($file, array('key'=>$rolling));
$str = '<?php return array(';
foreach ($res as $key => $value){
// '\'' 单引号转义
//$str .= '\''.$key.'\''.'=>'.'\''.$value.'\''.',';
if (is_array($value)){
$str.= '\''.$key.'\''.'=>array(';
foreach ($value as $k=>$v){
$str.='\''.$k.'\''.'=>'.'\''.$v.'\''.',';
}
$str.='),';
}else{
$str.='\''.$key.'\''.'=>'.'\''.$value.'\''.',';
}
};
$str .= '); ';
$upfile =file_put_contents($path, $str);//写入文件
|