我有以下课程:
class Config
{
public static $config;
public static function load() {
$connection = new DBConnection();
try {
$sql = 'SELECT `json`
FROM `general`
WHERE `title` = "config"
LIMIT 1';
$stmt = $connection->dbh->prepare($sql);
$stmt->execute();
self::$config = $stmt->fetch(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo $e->getMessage();
echo $e->getTraceAsString();
}
$connection->disconnect();
self::$config = json_decode(self::$config['json'], true);
}
}
我从数据库中检索到的信息是一种表示JSON的字符串格式。
在我使用json_decode将$ config从STRING转换为ARRAY之后,它在没有任何原因的情况下向数组的末尾添加了另一个“ NULL”单元格...
我试图在类外对变量$ config进行解码,并将其返回而没有“ NULL”。但是我希望它在类本身中执行解码。
存储的JSON:
{
"settings": {
"general": {
"web_name": "WEBSITE",
"url": "website.com",
"max_login_attempts": 3,
"login_block_time": 15,
"errors_mode": true,
"time_zone": "",
"max_events": 35,
"max_notifications": 35
},
"email": {
"host": "smtp-relay.gmail.com",
"port": 587,
"username": "",
"password": ""
},
"vars": {
"general": {
"web_name": "[web_name]",
"url": "[web_url]",
"max_login_attempts": "[max_login_attempts]",
"login_block_time": "[login_block_time]"
},
"dynamic": {
"admin_full_name": "[admin_full_name]",
"admin_email_address": "[admin_email_address]",
"admin_username": "[admin_username]",
"admin_id": "[admin_id]",
"admin_login_attempt_ip": "[admin_login_attempt_ip]"
}
}
}
}
错误的结果(数组的var_dump)-如果难以阅读,请先查看此后的代码:
array(1) {
["settings"]=> array(5) {
["general"]=> array(8) {
["web_name"]=> string(7) "WEBSITE"
["url"]=> string(11) "website.com"
["max_login_attempts"]=> int(3)
["login_block_time"]=> int(15)
["errors_mode"]=> bool(true)
["time_zone"]=> string(0) ""
["max_events"]=> int(35)
["max_notifications"]=> int(35)
}
["email"]=> array(4) {
["host"]=> string(20) "smtp-relay.gmail.com"
["port"]=> int(587)
["username"]=> string(0) ""
["password"]=> string(0) ""
}
["vars"]=> array(2) {
["general"]=> array(4) {
["web_name"]=> string(10) "[web_name]"
["url"]=> string(9) "[web_url]"
["max_login_attempts"]=> string(20) "[max_login_attempts]"
["login_block_time"]=> string(18) "[login_block_time]"
}
["dynamic"]=> array(5) {
["admin_full_name"]=> string(17) "[admin_full_name]"
["admin_email_address"]=> string(21) "[admin_email_address]"
["admin_username"]=> string(16) "[admin_username]"
["admin_id"]=> string(10) "[admin_id]"
["admin_login_attempt_ip"]=> string(24) "[admin_login_attempt_ip]"
}
}
[""]=> NULL
}
}
在对数组进行json_encode后的错误结果(为方便起见)
{
"settings": {
"general": {
"web_name": "WEBSITE",
"url": "website.com",
"max_login_attempts": 3,
"login_block_time": 15,
"errors_mode": true,
"time_zone": "",
"max_events": 35,
"max_notifications": 35
},
"email": {
"host": "smtp-relay.gmail.com",
"port": 587,
"username": "",
"password": ""
},
"vars": {
"general": {
"web_name": "[web_name]",
"url": "[web_url]",
"max_login_attempts": "[max_login_attempts]",
"login_block_time": "[login_block_time]"
},
"dynamic": {
"admin_full_name": "[admin_full_name]",
"admin_email_address": "[admin_email_address]",
"admin_username": "[admin_username]",
"admin_id": "[admin_id]",
"admin_login_attempt_ip": "[admin_login_attempt_ip]"
}
},
"": null **// --> Where does it come from???**
}
}