$form_state does not follow same array structure as $form I built a form in a form plugin like so.

public function buildForm(array $form, FormStateInterface $form_state) {
$array[0] = 'value1';
$array[1] = 'value2';
$array[2] = 'value3';
$array[3] = 'value4';

$form['field_1'] = [
    '#type' =&gt; 'value',
    '#value' =&gt; 'test value',
];

foreach($array as $item){
    $form['fields'][$item] = [
        '#type' =&gt; 'textarea',
        '#title' =&gt; $item,
    ];
}
    $form['actions'] = [
        '#type' =&gt; 'actions',
    ];

    $form['actions']['submit'] = [
        '#type'  =&gt; 'submit',
        '#value' =&gt; t('Submit'),
    ];

    return $form;
}


The form is built just fine and I can return the values upon submit like so,

public function submitForm(array&amp;$form, FormStateInterface $form_state) {
    die(print_r($form_state-&gt;getValues()));
}


This is part of the output I receive

[field_1] =&gt; test value
[value1] =&gt; User input for value 1 text area
[value2] =&gt; User input for value 2 text area
[value3] =&gt; User input for value 3 text area
[value4] =&gt; User input for value 4 text area
[submit] =&gt; Drupal\Core\StringTranslation\TranslatableMarkup Object


The issue is, I want value1,value2,value3, and value4 to appear as children in the ['fields'] part of the array.

To demonstrate, I'd want the output to appear like so.

[field_1] =&gt; test value
[fields] =&gt; [
    [value1] =&gt; User input for value 1 text area
    [value2] =&gt; User input for value 2 text area
    [value3] =&gt; User input for value 3 text area
    [value4] =&gt; User input for value 4 text area
]
[submit] =&gt; Drupal\Core\StringTranslation\TranslatableMarkup Object


The ultimate goal is to be able to use a foreach loop on the fields array, like this

foreach($fields as $key =&gt; $value){
    print_r('This is the key: '.$key.' and this is the value: '.$value);
}


I also attempted declaring 'fields' as a container first like so

$form['fields'] = [
    '#type' =&gt; 'container',
];


but it has no effect. How would I group form elements into an array when parsing the form state?
If you group the fields below $form['fields'] you also have to set #tree at the top of the array:

$form['fields']['#tree'] = TRUE;


Then the values will be structured in the same way as the form elements.