How to create a node with mulitple field-collection items? I've a content type having filed collection item (3 fields of 5 sets). Created a custom module form, when user submits that form, it should create a node with all 5 set field collection items.

So far tried the following code. it's working for 1st set but not for all.

$values = array(
  'type' =&gt; 'ct_machine_name', // Content Type machine name
  'uid' =&gt; $uid,
  'status' =&gt; 1,
  'promote' =&gt; 0,
  'title' =&gt; 'xxxx',
);
$entity = entity_create('node', $values);
$ewrapper = entity_metadata_wrapper('node', $entity);
$ewrapper-&gt;save();
$node = node_load($entity-&gt;nid);

$fc_val= entity_create('field_collection_item', array('field_name' =&gt; 'field_fc_mcnename'));
// Attach to the node
$fc_val-&gt;setHostEntity('node', $node);
$fc_val-&gt;field_field1[LANGUAGE_NONE][0]['value'] = $val1;
$fc_val-&gt;field_field2[LANGUAGE_NONE][0]['value'] = $val2;
$fc_val-&gt;field_field3[LANGUAGE_NONE][0]['value'] = $val3;

// Save field-collection item.
$fc_val-&gt;save();


The above code working. But I need to loop through it and save all field collection items in an user submitted order.

I tired as follows, but doesn't work. (example)

for ($i=0; $i&lt;=4; $i++) {
$fc_val-&gt;field_field1[LANGUAGE_NONE][$i]['value'] = $val1;
$fc_val-&gt;field_field2[LANGUAGE_NONE][$i]['value'] = $val2;
$fc_val-&gt;field_field3[LANGUAGE_NONE][$i]['value'] = $val3;
}
$fc_val-&gt;save();


Also tried

for ($i=0; $i&lt;=4; $i++) {
$fc_val-&gt;field_field1[LANGUAGE_NONE][0]['value'] = $val1;
$fc_val-&gt;field_field2[LANGUAGE_NONE][0]['value'] = $val2;
$fc_val-&gt;field_field3[LANGUAGE_NONE][0]['value'] = $val3;
$fc_val-&gt;save();
}


Any inputs or suggestions would be much appreciated.
Try This:
Example for field1 and repeat it with other fields.



$entity_fc_val = entity_metadata_wrapper('field_collection_item', $fc_val);
$entity_fc_val->field_field1->set(array('value' => $val1));