how to get all the last level term id's of taxonomy in drupal 7?
&lt;?php
function get_term_depth($tid) {
  $limit = 5; // level of your term 
  $depth = 0; 
  while ($parent = db_select('taxonomy_term_hierarchy', 't') -&gt;condition('tid', $tid, '=') -&gt;fields('t') -&gt;execute() -&gt;fetchAssoc()) {
    $depth ++;
    $tid = $parent['parent'];
    if ($depth &gt; $limit) {
      break;
    }
  }
  return $depth;
}
?&gt;