[wp-hackers] Removing empty values from serialized array
Dan Gayle
dangayle at gmail.com
Thu Aug 5 19:04:29 UTC 2010
On Thu, Aug 5, 2010 at 11:14 AM, Andrew Nacin <wp at andrewnacin.com> wrote:
> In addition to Otto's excellent advice for using $input and $output, here's
> some additional background:
>
> Checkboxes only send data if they are checked. The simplest way to handle
> this normally is to use empty() -- $checkbox_value = !empty(
> $_POST['checkbox_field'] );. If you ever need to remove keys with empty
> values from an array, simply pass the array through array_filter() without a
> callback.
Here's my resultant validation callback, and it works perfectly. I
suppose the foreach could have been in a callback on the array_filter
function, but this is just as easy. Thanks for your help!
function pms_options_validate($options) {
$allowed = array('option1','affiliateID');
foreach ($options as $key => $value):
if (in_array($key,$allowed)) $options[$key] = $value;
endforeach;
$options['option1'] = (isset($options['option1']) &&
($options['option1'] == 1)) ? $options['option1'] : NULL;
$options['affiliateID'] = (isset($options['affiliateID']) ) ?
(int) $options['affiliateID'] : NULL;
return array_filter($options);
}
More information about the wp-hackers
mailing list