Error while creting field help needed I am trying to create a field  in hook_install() using the 
following code. 

$field = array(     
  'field_name' =&gt; 'unit_count',
  'type' =&gt; 'text',
  'cardinality' =&gt; FIELD_CARDINALITY_UNLIMITED,
);

field_create_field($field);   

$instances = array();

$instances['unit_count'] = array(
  'field_name' =&gt; 'unit_count',
  'label' =&gt; $t('unit_count'),
  'description' =&gt; $t('unit_count'),
  'widget' =&gt; array(
    'type' =&gt; 'text_textfield',
  ),
  'required' =&gt; TRUE,
  'settings' =&gt; array(
    'text_processing' =&gt; 0,
  ),
);

foreach ($instances as $instance) {
  $instance['entity_type'] = 'node';
  $instance['bundle'] = 'first_example_bundle';
  field_create_instance($instance);
}


I am getting the following error:


  Notice: Undefined index: id in field_create_field() (line 173 of /Library/WebServer/Sites/Dev/Public/Peirce/modules/field/field.crud.inc).


What is wrong with the code I am using?
Im not sure what your error is. Every field has different widget settings and differing options ... keeping all the options you can set straight in my head is beyond me ... I have created fields in install profiles and using hook_install.  I tend to build the fields using the Field UI found at Structure -&gt; Content Type -&gt; then add/edit Fields then export those settings to code for use in a module.

I found this blog post immensely helpful:

Exporting and creating field definitions in Drupal 7

From the blog post you can get a field and its instance config with:

$field_data = field_info_field('field_article_image');
$instance_data = field_info_instance('node', 'field_article_image', 'article');


You can create a field and instance(s) in PHP using the Field API:

field_create_field($field_data);
field_create_instance($instance_data);


You can use Devel or Drush with drush php-script or eval and the following code nabbed from the blog post. It will generate the above 2 lines of code with the $config and $instance data all filled out for you to use in your custom code:

$entity_type = '';
$field_name = 'field_';
$bundle_name = '';

$info_config = field_info_field($field_name);
$info_instance = field_info_instance($entity_type, $field_name, $bundle_name);
unset($info_config['id']);
unset($info_instance['id'], $info_instance['field_id']);
include_once DRUPAL_ROOT . '/includes/utility.inc';
$output = "field_create_field(" . drupal_var_export($info_config) . ");\n";
$output .= "field_create_instance(" . drupal_var_export($info_instance) . ");";
drupal_set_message("&lt;textarea rows=30 style=\"width: 100%;\"&gt;". $output .'&lt;/textarea&gt;');


EDIT:

Once you see a valid export from an example field you are trying to create. It can be easy to re-use, tweak, or fix small parts of that field configuration to use in the same project or other projects. But I never sit down from scratch and try to code it all by hand for every field.