Initialize an Associative Array with Key Names but Empty Values Initialize an Associative Array with Key Names but Empty Values php php

Initialize an Associative Array with Key Names but Empty Values


What you have is the most clear option.

But you could shorten it using array_fill_keys, like this:

$database = array_fill_keys(  array('dbdriver', 'dbhost', 'dbname', 'dbuser', 'dbpass'), '');

But if the user has to fill the values anyway, you can just leave the array empty, and just provide the example code in index.php. The keys will automatically be added when you assign a value.


First file:

class config {    public static $database = array();}

Other file:

config::$database = array(    'driver' => 'mysql',    'dbhost' => 'localhost',    'dbname' => 'test_database',    'dbuser' => 'testing',    'dbpass' => 'P@$$w0rd');