0

I am trying to set up a structure where an associative array holds key/value data for a template of mine.

I want to be able to apply default data, but the below code has some obvious problems. First of all, it's a hassle writing that repetitive stuff. Second - the test for null doesn't work if the key isn't defined at all, then you just get an error saying that key 'background_color' is not defined.

How would I best go about structuring something like this?

//Defaults for this template
$default = array();
$default['background_color'] = '#ffffff';
$default['background_image'] = '';
$default['background_opacity'] = '1.0';
$default['background_repeat'] = '';
$default['background_position-horizontal'] = 'left';
$default['background_position-vertical'] = 'top';
$default['background_size'] = '';

if($data['background_color'] == null) { $data['background_color'] = $default['background_color']; }
if($data['background_image'] == null) { $data['background_image'] = $default['background_image']; }
if($data['background_opacity'] == null) { $data['background_opacity'] = $default['background_opacity']; }
if($data['background_repeat'] == null) { $data['background_repeat'] = $default['background_repeat']; }
if($data['background_position-horizontal'] == null) { $data['background_position-horizontal'] = $default['background_position-horizontal']; }
if($data['background_position-vertical'] == null) { $data['background_position-vertical'] = $default['background_position-vertical']; }
if($data['background_size'] == null) { $data['background_size'] = $default['background_size']; }

3 Answers 3

1

Use isset instead, and walk through the defaults with a foreach

foreach ($default as $key => $val) {
    if (!isset($data[$key]) {
        $data[$key] = $val;
    }
}

Isset does return false if the value is null, so you don't need an extra check for null.

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

Comments

1
//Defaults for this template
$default = array();
$default['background_color'] = '#ffffff';
$default['background_image'] = '';
$default['background_opacity'] = '1.0';
$default['background_repeat'] = '';
$default['background_position-horizontal'] = 'left';
$default['background_position-vertical'] = 'top';
$default['background_size'] = '';

foreach ($default as $k => $v) {
    if (!isset($data[$k])) {
        $data[$k] = $default[$k];
    }
}

4 Comments

Oops, well better to check it twice than to forget it ;) But I updated it accordingly
I modified your answer to use a ternary as well, it's a bit more concise
@BrandonWamboldt disagree about using ternary, it's unnecessary and less efficient than using an if statement. no reason to reassign $data[$k] to $data[$k]
@FuzzyTree Yeah, you're right, I've rolled back the edit
0

The way I always do a 'default' scenario is:

$data = (array)$data + (array)$default;

2 Comments

From what I have tested so far, this answer best suits my needs. I propably didn't specify clearly in the initial question that I need all my default values present, even if the $data array doesn't have them.
Glad that you found it useful. I picked up this method doing things in Javascript and found it to be the cleanest way of doing this type operation. Biggest thing to remember is that the earliest arrays always takes priority over the later :)

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.