You can and should do it in a more simple way:
In this example you can see the $tags variable, it get the tags of the post compare them and shows 7 posts (the showposts parameter) also makes sure that it won’t show the original post (post__not_in parameter).
<?php // related posts based on first tag of current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo '<h3>Related Posts</h3>';
$first_tag = $tags[0]->term_id;
$args = array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts' => 7, // how many posts?
'caller_get_posts' => 1
);
$my_query = new WP_Query($args);
if ($my_query->have_posts()) { ?>
<ul>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="https://wordpress.stackexchange.com/questions/232088/<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
<?php } ?>
<?php } ?>