Remove duplicates from echo output in PHP [closed]

This is more of a PHP programming question than specific to WordPress, but you could do something like this.

This keeps a track of which colors have been shown in $shownColors array and doesn’t show the same one twice.

function loop_display_variation_attribute_and_thumbnail() {
    global $product;
    if( $product->is_type('variable') ){

        $shownColors = Array();

        foreach ( $product->get_visible_children() as $variation_id ){
            // Get an instance of the Product_Variation object
            $variation = wc_get_product( $variation_id );

            // Get "color" product attribute term name value
            $color = $variation->get_attribute('pa_color');

            if( ! empty($color) && !in_array($color, $shownColors) ){
            // Display "color" product attribute term name value
                echo $color;

                // Display the product thumbnail with a defined size (here 30 x 30 pixels)
                echo $variation->get_image( array(30, 30) );

                $shownColors[] = $color;  // add this to colors we've shown
            }
        }
    }
}