Create node programmatically with menu entries
The code you posted seems a bit frail at places, but if it works for you, we can focus on your specific question.

The main problem is that you are not adding the $menu array to your node object before saving it. You would only need to add it to the object before you save it, similar to this:

$node-&gt;menu = $menu;
...
node_save($node);


Cheers and good luck!

P.S.: You can check out this thread for some code examples https://stackoverflow.com/q/2603289/5875098
P.S.2: To get all the terms directly from the $node object:

$terms = array();
$language = 'und'; // or will be provided by some Drupal hooks
foreach ($node-&gt;field_tags[$language] as $delta =&gt; $value) {
  $termObj = taxonomy_term_load($value['tid']);
  $terms[] = $termObj-&gt;name;
}


You can replace the $terms = explode... in your code with this snippet.