Sometimes we need to extract only specific level of taxonomy terms.
And in Drupal we can do it on the next way.

For example we have a some taxonomy terms tree like this:

  • Drinks
    • Alcohol Drinks
      • Bear
      • Whiskey
      • Vodka
    • Alcohol Free Drinks
      • Coca Cola
      • Pepsi
      • Milk
  • Fruits
    • Fruits For Losing Weight
      • Apricots
      • Blackberries
      • Cantaloupe
    • Fruits For Dogs
      • Strawberry
      • Watermelon
      • Orange
  • Foods
    • Fast Foods
      • Hamburger
      • Pizza
      • KFC
    • Healthy Foods
      • Almonds
      • Oily Fish
      • Avocados

Extract only first level

$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
  'some_vocabulary', // This is your taxonomy term vocabulary (machine name).
  0,                 // This is "tid" of parent. Set "0" to get all.
  1,                 // Get only 1st level.
  TRUE               // Get full load of taxonomy term entity.
);

$result = array();

foreach ($tree as $term) {
  $result[] = $term->getTerm();
}

print_r($result);

Array
(
  [0] => Drinks
  [1] => Fruits
  [2] => Foods
)

Extract 1st and 2nd levels

$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
  'some_vocabulary', // This is your taxonomy term vocabulary (machine name).
  0,                 // This is "tid" of parent. Set "0" to get all.
  2,                 // Get terms from 1st and 2nd levels.
  TRUE               // Get full load of taxonomy term entity.
);

$result = array();

foreach ($tree as $term) {
  $result[] = $term->getTerm();
}

print_r($result);

Array
(
  [0] => Drinks
  [1] => Alcohol Drinks
  [2] => Alcohol Free Drinks
  [3] => Fruits
  [4] => Fruits For Losing Weight
  [5] => Fruits For Dogs
  [6] => Foods
  [7] => Fast Foods
  [8] => Healthy Foods
)

Extract only 2nd level

$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
  'some_vocabulary', // This is your taxonomy term vocabulary (machine name).
  0,                 // This is "tid" of parent. Set "0" to get all.
  2,                 // Get terms from 1st and 2nd levels.
  TRUE               // Get full load of taxonomy term entity.
);

$result = array();

foreach ($tree as $term) {
  // We get 1st and 2nd levels, also we check parents (only 2nd level has parents).
  if (!empty(\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadParents($term->id()))) {
    $result[] = $term->getTerm();
  }
}

print_r($result);

Array
(
  [0] => Alcohol Drinks
  [1] => Alcohol Free Drinks
  [2] => Fruits For Losing Weight
  [3] => Fruits For Dogs
  [4] => Fast Foods
  [5] => Healthy Foods
)

Extract 2nd and 3rd levels

$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
  'some_vocabulary', // This is your taxonomy term vocabulary (machine name).
  0,                 // This is "tid" of parent. Set "0" to get all.
  3,                 // Get terms from 1st, 2nd and 3rd levels.
  TRUE               // Get full load of taxonomy term entity.
);

$result = array();

foreach ($tree as $term) {
  // We get 2nd and 3rd levels, also we check parents (1st level does not have parents).
  if (empty(\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadParents($term->id()))) {
    $result[] = $term->getTerm();
  }
}

print_r($result);

Array
(
  [0] => Alcohol Drinks
  [1] => Bear
  [2] => Whiskey
  [3] => Vodka
  [4] => Alcohol Free Drinks
  [5] => Coca Cola
  [6] => Pepsi
  [7] => Milk
  [8] => Fruits For Losing Weight
  [9] => Apricots
  [10] => Blackberries
  [11] => Cantaloupe
  [12] => Fruits For Dogs
  [13] => Strawberry
  [14] => Watermelon
  [15] => Orange
  [16] => Fast Foods
  [17] => Hamburger
  [18] => Pizza
  [19] => KFC
  [20] => Healthy Foods
  [21] => Almonds
  [22] => Oily Fish
  [23] => Avocados
)

Extract only last level

$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree(
  'some_vocabulary', // This is your taxonomy term vocabulary (machine name).
  0,                 // This is "tid" of parent. Set "0" to get all.
  NULL,              // Get all levels.
  TRUE               // Get full load of taxonomy term entity.
);

$result = array();

foreach ($tree as $term) {
  if (empty(\Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadChildren($term->id()))) {
    $result[] = $term->getTerm();
  }
}

print_r($result);

Array
(
  [1] => Bear
  [2] => Whiskey
  [3] => Vodka
  [4] => Coca Cola
  [5] => Pepsi
  [6] => Milk
  [7] => Apricots
  [8] => Blackberries
  [9] => Cantaloupe
  [10] => Strawberry
  [11] => Watermelon
  [12] => Orange
  [13] => Hamburger
  [14] => Pizza
  [15] => KFC
  [16] => Almonds
  [17] => Oily Fish
  [18] => Avocados
)
# drupal8 # taxonomy_term