Get categories that has posts with a certain tag

I have a tag template where I list all the products based on category and tag combined. At the bottom of this page I want to show the visitor all the other categories that have products with the same tag (product brand in this case).

I've tried with the code below but nothing gets returned. Am I thinking about it in the wrong way maybe?

$terms = get_terms( array(
    'taxonomy' = 'category',
) );

// Loop through them
foreach($terms as $term) {
  // Get the posts in that category with the required tag
  $args = array(
    'category_name'    = $term-name,
    'tax_query'      = array(
        array(
            'taxonomy'  = 'post_tag',
            'field'     = 'slug',
            'terms'     = $tag_name
        )
    )
  );
  $posts_array = get_posts( $args );
  foreach ($posts_array as $value) {
    echo $value[post_title];
  }
}

Topic tax-query tags Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.