How do I set group fields in pairs?
You could add a fieldset for each person, something like this should work:

for ($i = 0; $i &lt; $num_names; $i++) {
  $form['names_fieldset']['person_' . $i] = [
    '#type' =&gt; 'fieldset',
    '#title' =&gt; $this-&gt;t('Person ' . $i),
  ];

  $form['names_fieldset']['person_' . $i]['name'] = [
    '#type' =&gt; 'textfield',
    '#title' =&gt; $this-&gt;t('Name'),
  ];
  $form['names_fieldset']['person_' . $i]['surname'] = [
    '#type' =&gt; 'textfield',
    '#title' =&gt; $this-&gt;t('Surname'),
  ];
}


And then alter your remove callback and a few other bits to match