Return to Snippet

Revision: 66044
at March 4, 2014 07:13 by wyattstorch42


Initial Code
<?php
extract(
	array_intersect_key(
		array_merge(
			$defaults = array (
				'default' => 'options',
				'and' => 'values',
				'go' => 'here',
				'required' => true,
				'filter_function' => null,
				'error_string' => 'An error occurred: %s'
			),
			$options
		),
		$defaults
	)
);

// Function Version

/**
 * Takes an associative array of options and their default settings.
 * Removes entries of options that do not exist in the defaults.
 * Returns an array of options with the defaults where options do not
 * override the defaults.
 * @param array $options The user's options
 * @param array $defaults The default options
 * @return array The filtered and overridden options
 */
function safe_options ($options, $defaults) {
	return array_intersect_key(array_merge($defaults, $options), $defaults);
}

extract(safe_options(
	$options,
	array (
		'default' => 'options',
		'and' => 'values',
		'go' => 'here',
		'required' => true,
		'filter_function' => null,
		'error_string' => 'An error occurred: %s'
	)
));
print $default.$and.$go.$required.$filter_function.$error_string;
?>

Initial URL

                                

Initial Description
Take an associative array of options and their default settings. Remove entries of options that do not exist in the default options. Return a safely filtered and overridden array of options.

Initial Title
Override defaults with options (PHP associative arrays)

Initial Tags
php

Initial Language
PHP