How to set value for multivalue field in drupal 8 programmatically I was saving poll choices Programmatically in Drupal 8. I have used Drupal8 Poll choices module.

$node_poll = Poll::load($pollid);
//print_r($node_poll);exit;
$i = array(13,14,15);
foreach($i as $key =&gt; $value){
  $node_poll-&gt;choice-&gt;setValue(
        [
            ['target_id' =&gt; $value]
        ]
  );
// But I want to save multiple target ids. now this is saving 15 all the time.

}
$node_poll-&gt;question-&gt;setValue([
  'value' =&gt; 'test',
]);
$node_poll-&gt;save();


The following snippet is working fine.

$node_poll-&gt;choice-&gt;setValue(
        [
            ['target_id' =&gt; 13]
       ],
       [
            ['target_id' =&gt; 14]
       ],
        [
            ['target_id' =&gt; 15]
       ]
    );


How can I, make target ids to be saved dynamically?
I would rewrite your code like this.
N.B.: This an edited version of the original answer, following some helpful points coming from the comments.

$poll = Poll::load($pollid);

$poll-&gt;question-&gt;setValue(['value' =&gt; 'test']);

$target_ids = array(13,14,15);
foreach($target_ids as $target_id){
  $poll-&gt;choice-&gt;appendItem($target_id);
}
$poll-&gt;save();


Hope this does it!