How to Remove Duplicate Values From a Loop
Remove duplicate values from a while loop. Simply you can push all elements to one array and then remove the duplicates and then get values as you need. Use your foreach to assign the correct values to an array. So It is very complicated to remove duplicate values from an array within foreach loop.
<?php
$args = array(
'post_type' => 'posts',
'order' => 'ASC',
'orderby' => 'name'
);
$wp_query = new WP_Query( $args );
$unique_location = array();
if( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
$location = get_the_field('location');
if( ! in_array( $location, $unique_location ) ) :
$unique_location[] = $location; ?>
<option value="<?php echo $location; ?>"><?php echo $location; ?></option>
<?php
endif;
endwhile;
endif;
?>