为什么我的首选项数据以数组而不是字符串的形式返回?

Why is my preferences data being returned as an array instead of a string?

我正在使用此配置 class 让我更容易读出我的偏好。

<?php

class Config {
    public static function get($path = null) {
        if ($path){
            $config = $GLOBALS['config'];
            $path = explode('/', $path);

            foreach($path as $bit) {
                if(isset($config[$bit])) {
                    $config = $config[$bit]; 
                }
            }

            return $config;
        }

        return false;
    }
}

现在,我应该可以通过在我的脚本中使用这一行来获取配置:

echo Config::get('settings/main_color');

我的首选项在 JSON 文件中,但存储在 $GLOBALS['config'] 中的数组如下所示:

Array ( 
    [mysql] => Array ( 
        [host] => localhost:3307 
        [username] => root 
        [password] => usbw 
        [db] => webshop )
    [remember] => Array ( 
        [cookie_name] => hash 
        [cookie_expiry] => 604800 ) 
        [sessions] => Array ( 
        [session_name] => user 
        [token_name] => token ) 
    [settings] => Array ( 
        [main_color] => #069CDE 
        [front_page_cat] => Best Verkocht,Populaire Producten 
        [title_block_first] => GRATIS verzending van €50,- 
        [title_block_second] => Vandaag besteld morgen in huis! ) 
    [statics] => Array ( 
        [header] => enabled 
        [title_block] => enabled 
        [menu] => enabled 
        [slideshow] => enabled 
        [left_box] => enabled 
        [email_block] => enabled 
        [footer] => enabled 
        [keurmerken] => enabled 
        [copyright] => enabled ) 
)

现在,当我尝试在我的脚本中达到一个偏好时。它说我的字符串是一个数组。所以我用 print_r 来显示数组。那么结果如下:

print_r(Config::get('settings/main_color'));

Array ( [header] => enabled [title_block] => enabled [menu] => enabled [slideshow] => enabled [left_box] => enabled [email_block] => enabled [footer] => enabled [keurmerken] => enabled [copyright] => enabled )

我的脚本哪里出错了?

如果确实如此,您的数组是结构化的,如上所示,这应该可以工作

<?php

class Config {
public static function get($path = null) {
    if ($path){
        $config = $GLOBALS['config'];
        $path = explode('/', $path);

        $parent = $path[0];
        $child = $path[1];

        if(isset($config[$parent][$child])) {
            $config = $config[$parent][$child]; 
        }

        return $config;
    }

    return false;
}
}

希望对您有所帮助。