Multiple fields that are generated dynamically in forms
You can use session to store your data like:

$session = new \Symfony\Component\HttpFoundation\Session\Session();
$session-&gt;set('counter', $session-&gt;get('counter',0) + 1);
$counter =$session-&gt;get('counter');

$form['counter'] = array(
   '#type' =&gt; 'hidden',
   '#value' =&gt; $counter
);


Tuto : http://www.drupal8.ovh/en/tutoriels/25/save-user-temporary-data-on-session