Getting child term's Tid not the parent's I created a form via Drupal 8 API, and I am creating a dropdown of a taxonomy terms which has a parent-child hierarchy. But when i select the taxonomy term, it displays term's parent's tid. This is how i try to display the dropdown:

$option = [];
$terms = \Drupal::service('entity_type.manager')-&gt;getStorage("taxonomy_term")-&gt;loadTree('collections', $parent = 0, $max_depth = 3, $load_entities = FALSE);

foreach ($terms as $term) {
  if($term-&gt;depth == 0){
    $term = $term-&gt;name;
  }
  elseif ($term-&gt;depth == 1) {
    $term = '-'.$term-&gt;name;
  }
  elseif ($term-&gt;depth == 2){
   $term = '--'.$term-&gt;name;
  }
  elseif ($term-&gt;depth == 3){
    $term = '---'.$term-&gt;name;
  }
  elseif ($term-&gt;depth == 4){
    $term = '----'.$term-&gt;name;
  }
  $option[] = $term;
}

$form['collection'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('In Collection:'),
  '#empty_option' =&gt; t(' '),
  '#options' =&gt; $option,
);


I have been trying to get the term's tid not the parent's tid, can anyone point out what am doing wrong? Note:I am not super experienced with drupal 8
For the further references: I changed my code to this:

$option = [];
$terms = \Drupal::service('entity_type.manager')-&gt;getStorage("taxonomy_term")-&gt;loadTree('collections', $parent = 0, $max_depth = 3, $load_entities = FALSE);

foreach ($terms as $term) {
$key = $term-&gt;id;

if($term-&gt;depth == 0){
  $term = $term-&gt;name;
}
elseif ($term-&gt;depth == 1) {
  $term = '-'.$term-&gt;name;
}
elseif ($term-&gt;depth == 2){
 $term = '--'.$term-&gt;name;
}
elseif ($term-&gt;depth == 3){
  $term = '---'.$term-&gt;name;
}
elseif ($term-&gt;depth == 4){
  $term = '----'.$term-&gt;name;
}
 $option[$key] = $term;
}

$form['collection'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('In Collection:'),
  '#empty_option' =&gt; t(' '),
  '#options' =&gt; $option,
);


Sorry if I wasted your time.