Store array of ids in a field programmatically? Devel noob here. I am writing a module where I need to stash a list of entity ids in a field on a different entity. I've created a field on an entity type programmatically and have it set as field type list(integer), but I'm stuck at actually working with the field data.

How can I add/edit entity ids in the field list programmatically?

For context, this is a Commerce submodule and I am adding to a field on the line item created after submitting an add to cart form. Using dsm($form_data) in a submit handler I added to the add to cart form, I see that the line item object is already stored in $form_data['line_item'] and the data I need to get to would be stored at $form_data['line_item']-&gt;field_entity_ids. By this point, I already have my array of ids, I just need some guidance on how to store them properly in the field. 

Thanks in advance! :)
Consider using hook_entity_presave() to populate any auto-generated fields.

In your case, you could do something like:

/**
 * Implements hook_entity_presave().
 */
function example_product_entity_presave($entity, $type) {
  // Set the values for our computed fields.
  switch ($type) {
    case 'commerce_line_item':
      // @todo: get the value for $foo_ids.
      foreach ($foo_ids as $foo_id) {
        $entity-&gt;field_entity_ids[LANGUAGE_NONE][] = array(
          'value' =&gt; $foo_id,
        );
      }
      break;
  }
}


If you intend to reference any existing entity, you might want to use the Entity Reference module and use an entity reference field instead of an integer field.