使用键名但为空值初始化关联数组

问题描述 投票:53回答:2

我在书中或网络上找不到任何示例,这些示例仅描述了如何使用名称(具有空值)正确初始化关联数组-除非当然是正确的方法(?)

感觉好像还有另一种更有效的方法:

config.php

class config {
    public static $database = array (
        'dbdriver' => '',
        'dbhost' => '',
        'dbname' => '',
        'dbuser' => '',
        'dbpass' => ''
    );
}

// Is this the right way to initialize an Associative Array with blank values?
// I know it works fine, but it just seems ... longer than necessary.

index.php

require config.php

config::$database['dbdriver'] = 'mysql';
config::$database['dbhost'] = 'localhost';
config::$database['dbname'] = 'test_database';
config::$database['dbuser'] = 'testing';
config::$database['dbpass'] = 'P@$$w0rd';

// This code is irrelevant, only to show that the above array NEEDS to have Key
// names, but Values that will be filled in by a user via a form, or whatever.

任何建议,建议或指示,将不胜感激。谢谢。

php arrays initialization associative
2个回答
54
投票

您拥有的是最清晰的选择。


1
投票

第一个文件:

© www.soinside.com 2019 - 2024. All rights reserved.