Filtering assoc array returned from variable_get()

Events happening in the community are now at Drupal community events on www.drupal.org.
rgchi's picture

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?

Comments

I don't think u need a

MrMaksimize's picture

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: