array数组格式化[]

<?php
/**
 * @DATE:2021/3/12 0012
 * @TIME:22:12
 * @AUTHOR:sky
 * @Email:[email protected]
 * @File:Syssetting.php
 **/

namespace app\admin\controller;

use think\facade\Config;
class SysSetting extends Base
{
//    public function initialize()
//    {
//        parent::initialize(); // TODO: Change the autogenerated stub
//        event('CheckAuth');
//    }
    public function index(){

        if($this->request->isPost()){
            $data=$this->request->post();
            unset($data['file']);
            $res=$this->update_config($data);
            return json($res);
        }
        $_data=Config::get('system');
        return view('index',['data'=>$_data]);
    }



    private function update_config($new_config) {
       $config_file = config_path() . 'system.php';
        if (is_writable($config_file)) {
            $config = Config::get('system');
            foreach($config as $key=>$vo){
                $list[$key] = array_merge($vo,$new_config[$key]);
            }

            file_put_contents($config_file, "<?php \nreturn " . $this->varexport($list,true) . ";", LOCK_EX);

            $res=['status'=>true,'msg'=>'设置成功'];
        } else {

            $res=['status'=>false,'msg'=>'设置失败'];
        }
        return $res;
    }

    private  function varexport($expression, $return=FALSE) {
        $export = var_export($expression, TRUE);
        $patterns = [
            "/array \(/" => '[',
            "/^([ ]*)\)(,?)$/m" => '$1]$2',
            "/=>[ ]?\n[ ]+\[/" => '=> [',
            "/([ ]*)(\'[^\']+\') => ([\[\'])/" => '$1$2 => $3',
        ];
        $export = preg_replace(array_keys($patterns), array_values($patterns), $export);
        if ((bool)$return) return $export; else echo $export;
    }
}

猜你喜欢

转载自blog.csdn.net/u013373006/article/details/118098322