Store array of ids in a field programmatically?
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.