59

I cannot find any examples, in books or on the web, describing how one would properly initialize an associative array by name only (with empty values) - unless, of course, this IS the proper way(?)

It just feels as though there is another more efficient way to do this:

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.

Any recommendations, suggestions, or direction would be appreciated. Thanks.

2
  • Hey, not important but you've written 'dbname' -> '' where it should have been 'dbname' => '' - I don't have enough reputation to make the edit. Commented May 24, 2017 at 15:37
  • @Martha - I made the edit as you suggested. Commented May 14, 2020 at 14:41

2 Answers 2

66

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You have to do this outside of the class, though, since you can't call any functions in class variable declarations. That may result in more code, or initialization code appearing in places where you don't expect to see it.
@BoltClock Yeah, I wouldn't choose this option anyway. Those extra characters you need for 'normal' array initialization make it much clearer to me what the code does. I would leave it as it is. Just showing that if you want to, there are ways to do it. :) You could do this in a constructor, but not for a static class, of course.
Again, static, was also just an example scenario... But what I really was looking for was the shortened method to filling them. It seems that, although I got my answer, that the original is the best way to go - assuming that from the comments above. Thanks guys...
didn't know about array_fill_keys and although it has it's limits this is a very nice & efficient approach.
3

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'
);

1 Comment

That is hard coded, my second file was just an example - I needed the keys already defined.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.