'How to separate terms name with comma in an array?

I am trying to get taxonomies terms names separated with a comma. This is the code, but it separates taxonomies and terms with : If there are many terms they get separated with : instead of , If I delete the : symbol there are nothing in between taxonomy terms names.

<ul class='apt-product-terms'>
    <?php
        $_taxonomies = array(
          'taxname1'   =>   __( 'Taxname1', 'textstringdomain' ),
          'taxname2'   =>   __( 'Taxname2', 'textstringdomain' ),
          'taxname3'   =>   __( 'Taxname3', 'textstringdomain' ),
          'taxname4'   =>   __( 'Taxname4', 'textstringdomain' )
        );
      
      
        foreach ($_taxonomies as $taxonomy_slug => $taxonomy_name) {
            $terms = get_the_terms( $post->ID, $taxonomy_slug);
            if (is_array($terms) && count($terms) > 0) { ?>
               <li class='apt-tax-item'>
                  <span class='apt-term-name'><?php echo $taxonomy_name  ?></span>
                  <ul class='apt-tax-term-list'>
                     <?php foreach ( $terms as $term ) { ?>
                     <li class='apt-term-item'>: 
                        <a class='apt-term-link' href="<?php echo get_term_link($term); ?>"> <?php echo $term->name ?></a>           
                     </li>
                     <?php } ?>
                  </ul>
               </li>
            <?php            
            }
        }  
    ?>
</ul>

Where is the problem. I think it might be related to echo $term->name



Solution 1:[1]

Thanks to @Chris Haas example here https://3v4l.org/oZ7JA I modified my code in this way and now it works as I expected.

<ul class='apt-product-terms'>
    <?php
        $_taxonomies = array(
          'taxname1'   =>   __( 'Taxname1', 'textstringdomain' ),
          'taxname2'   =>   __( 'Taxname2', 'textstringdomain' ),
          'taxname3'   =>   __( 'Taxname3', 'textstringdomain' ),
          'taxname4'   =>   __( 'Taxname4', 'textstringdomain' )
        );
      
      
        foreach ($_taxonomies as $taxonomy_slug => $taxonomy_name) {
            $terms = get_the_terms( $post->ID, $taxonomy_slug);
            if (is_array($terms) && count($terms) > 0) { ?>
               <li class='apt-tax-item'>
                  <span class='apt-term-name'><?php echo $taxonomy_name  ?></span>:&nbsp
                  <ul class='apt-tax-term-list'>
                     <?php $idx = 0; 
                     foreach ( $terms as $term ) { 
                     $idx++; ?>
                     <li class='apt-term-item'>: 
                        <a class='apt-term-link' href="<?php echo get_term_link($term); ?>"> <?php echo $term->name;
    if($idx < count($terms)){
        echo ',&nbsp';
    } ?></a>           
                     </li>
                     <?php } ?>
                  </ul>
               </li>
            <?php            
            }
        }  
    ?>
</ul>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 twelvell