Form Alter Foreach Loop changes not sticking I'm trying to modify my exposed filter block to put each of the controls inside fieldsets, and make other modifications to them.

However, when I try to utilize a foreach loop to do this, none of my changes are 'sticking'.

foreach($form_state['input'] as $delta =&gt; $input) { 
    $collapsed = (empty($input) ? TRUE : FALSE);

    $form[$delta . '_fieldset'] = array(
        '#type' =&gt; 'fieldset',
        '#title' =&gt; t($form['#info']['filter-' . $delta]['label']),
        '#collapsible' =&gt; TRUE,
        '#collapsed' =&gt; $collapsed,
    );

    $form[$delta . '_fieldset'][$delta] = $form[$delta];
    unset($form[$delta]);
    unset($form['#info']['filter-' . $delta]);

}   


This wont work, but this will:

$delta = 'field_resource_type_tid';
$collapsed = (empty($form_state[$input][$delta]) ? TRUE : FALSE);

$form[$delta . '_fieldset'] = array(
    '#type' =&gt; 'fieldset',
    '#title' =&gt; t($form['#info']['filter-' . $delta]['label']),
    '#collapsible' =&gt; TRUE,
    '#collapsed' =&gt; $collapsed,
);

$form[$delta . '_fieldset'][$delta] = $form[$delta];
unset($form[$delta]);
unset($form['#info']['filter-' . $delta]);


I've been trying to find out why this wont work, but I can't for the life of my figure out what the form modifications wont stick when done inside this foreach loop.
I've finally managed to resolve my issue, but I'm not sure why. I decided to change what array I was using to iterate through the filter fields on my form. By switching to iterating through the form array instead of form_state, any changes I make to it now are sticking.

foreach($form['#info'] as $delta =&gt; $input) {
    $filtername = $input['value'];
    $collapsed = (empty($form_state['input'][$filtername]) ? TRUE : FALSE);

    $form[$filtername . '_fieldset'] = array(
        '#type' =&gt; 'fieldset',
        '#title' =&gt; t($form['#info']['filter-' . $filtername]['label']),
        '#collapsible' =&gt; TRUE,
        '#collapsed' =&gt; $collapsed,
    );

    $form[$filtername . '_fieldset']['' . $filtername] = $form[$filtername];
    unset($form[$filtername]);
    unset($form['#info']['filter-' . $filtername]);

}