Drupal Coding D7 - Taxonomy - How do I establish the hierarchy and associated fields for taxonomy terms?
Here is an example:

// create the Foo vocabulary
$vocabulary = new stdClass();
$vocabulary-&gt;name = "Foo Name";
$vocabulary-&gt;machine_name = "foo_vocab";
$vocabulary-&gt;description = "Foo Description";
taxonomy_vocabulary_save($vocabulary);
// annoyingly taxonomy_vocabulary_save doesn't return any information about the vocabulary
$voc_vid = db_query("SELECT v.vid FROM {taxonomy_vocabulary} v WHERE v.machine_name = 'foo_vocab'")-&gt;fetchField();
$terms = array(
  'bar',
  'bat',
);
foreach ($terms as $term_name) {
  $term = new stdClass();
  $term-&gt;vid = $voc_vid;
  $term-&gt;name = $term_name;
  taxonomy_term_save($term);
}