Filtering assoc array returned from variable_get()

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by rgchi on August 16, 2012 at 4:01pm

Given this checkboxes form field for a drupal module

$form['soc_eport_expireByFiletype'] = array(
'#type' => 'checkboxes',
'#title' => t('Select file extention'),
'#default_value' => variable_get('soc_eport', array('gif'=>'Gif')),
'#options' => array(
'gif' => t('GIF'),
'jpeg' => t('JPEG'),
'jpg'=> t('JPG'),
'png' => t('PNG'),
),

);

It produces this array:

Array
( [gif] => gif
[jpeg] => jpeg
[jpg] => 0
[png] => 0
)

I want to filter out anything that is a null (0). It seems that doing a array_filter() like this
$a = variable_get('soc_eport', array('gif'=>'Gif'));

$filtered_array = array_filter($a, function($b) {
return $b;
});

produces my desired result:

Array
(
[gif] => gif
[jpeg] => jpeg
)

Is there a better way?

Categories: ,

Comments

I don't think u need a

Posted by MrMaksimize on August 16, 2012 at 4:11pm

I don't think u need a callback function for array_filter. If you dont pass a callback function array_filter's default behavior is to filter out empty values. so just $filtered_array = array_filter($a);

Chicago

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /