Insert several lines in field collection programmatically I have a problem with field collection and entity reference fields. I have a field collection called 'field_co_intervenant' and i want to add several entities in this field collection and after saved my node. But i got an error with the index i think, because when the node is saved, there is just one reference entity in my field collection. You can see the code here : 

$intervenant is the array of my 'nid'.

if(!empty($intervenant)) {
    //Create entity
    $fieldcollection = entity_create('field_collection_item', array('field_name' =&gt; 'field__doc_fc_intervenant_tab'));
    //Attach entity
    $fieldcollection-&gt;setHostEntity('node', $node);
    //Fill field
    $i = 0;
    foreach ($intervenant as $val) {
        if($val != null) {
            $fieldcollection-&gt;field_fc_intervenant_tab[LANGUAGE_NONE][$i]['target_id'] = $val;
            $i++;
        }
    }

    //Save entity
    $fieldcollection-&gt;save();
}


How can i save all my values in the field collection and not just one ?

Thanks
First be sure that that field in your field collection field setting accept multi value then try save your item with 

  $node-&gt;field_fc_intervenant_tab['und'][] = array('value' =&gt; $field_collection_item-&gt;item_id,
    'revision_id' =&gt; $field_collection_item-&gt;revision_id,);


and finally be something like 

if(!empty($intervenant)) {    

    // you should have $node here
      $field_collection_item = NULL;
      $field_collection_item = entity_create('field_collection_item', array('field_name' =&gt; ''field__doc_fc_intervenant_tab '));
      $field_collection_item-&gt;setHostEntity('node', $node);
      $field_collection_item-&gt;save(TRUE);
          foreach ($intervenant as $val) {
             if($val != null) {
      $node-&gt;field_fc_intervenant_tab['und'][] = array('value' =&gt; $field_collection_item-&gt;item_id,
        'revision_id' =&gt; $field_collection_item-&gt;revision_id,);
}
}
    $fieldcollection-&gt;save();
}