hook_form_alter building dynamic textfields In hook_form_alter I am fetching data from the database. Some of the data has several textfields, lets say addresses. People can have several addresses.

How can I pass the count of rows to my form, and have it rendered with the row count?

I try to do the following in hook_form_alter

function personalinfo_form_alter(&amp;$form, &amp;$form_state, $form_id) {
    $i = 0;
    foreach ($arr as $value) {
        $form['personal'][$i]['address']['#value'] = $value;
        $i ++
    }
    $form_state['num_names'] = $i;
    }


And in my function personaladdresses($form, &amp;$form_state) , where the texfields are actually build

I want to use $form_state['num_names'] to do a for/while loop to build the textfields with the addresses.

But it seems that hook_form_alter is called AFTER the for/while loop, so it will never get the correct value from $form_state['num_names']

how can I pass a value to my form function before the for/while loop?
You should use following code in the form. It will display the array structure in $form_state['values'] in form_submit.

$form['#tree] = TRUE;