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']; }