小叮当的肚兜 2021-08-23 14:54:35 500次浏览 0条评论 0 0 0
/**
 * 数组转yaconf配置文件
 * arr2ini
 * @param array  $config
 * @param string $parent
 * @param int    $level
 * @return string
 */
function arr2ini(array $config, $parent = '',$level=0){
    $out = '';
    foreach ($config as $key => $value) {
        if (is_array($value)) {
            $level++;
            if($level==0){
                $out .= '[' . $key . ']' . PHP_EOL;
                $sec='';
            }else{
                if(empty($parent)){
                    $sec = $key.'.';
                }else{
                    $sec = $parent.$key.'.';
                }
            }
            $out .= arr2ini($value, $sec,$level);
        } else {
            if(is_numeric($value)){
                $out .= $parent."$key=$value" . PHP_EOL;
            }elseif(is_bool($value)){
                $out .= $parent."$key=".($value?1:0) . PHP_EOL;
            }else{
                $out .= $parent."$key='$value'" . PHP_EOL;
            }
            $level=0;
        }
    }
    return $out;
}

效果如下

components.user.on afterLogin.0='backend\models\User'
components.user.on afterLogin.1='afterLogin'
components.session.name='EasyDoAdminSession'
components.session.class='yii\web\DbSession'
components.session.timeout=86400
components.session.sessionTable='admin_user_session'
components.log.traceLevel=3
components.log.targets.0.class='yii\log\FileTarget'
components.log.targets.0.levels.0='error'
components.log.targets.0.levels.1='warning'
components.errorHandler.errorAction='site/error'
components.authManager.class='yii\rbac\DbManager'
components.authManager.defaultRoles.0='guest'
components.urlManager.enablePrettyUrl=1
components.urlManager.showScriptName=0
on beforeRequest.0='\common\components\autoLoad\Load'
on beforeRequest.1='run'
id='EasyDoAdmin'
......
    没有找到数据。
您需要登录后才可以评论。登录 | 立即注册