2

I am using Word Press with Advanced Custom Fields and have a loop to add a field (location) to an Options tag.

There are several posts with the same location, so if I loop normally I get results such as Toronto, Ottawa, Toronto, Montreal, Toronto, Toronto, Montreal, etc. I want it so it only has "Toronto, Ottawa, Montreal".

I tried to add the values to an array, then use array_unique(), but I do not know how to separate the values in the array.

My code -

<?php while( have_posts() ) : the_post(); ?>
    <?php while ( have_rows( 'location_info' ) ) : the_row(); ?>
        <?php $locations[] = get_sub_field( 'location' ); ?>
        <option><?php echo implode('', array_unique($locations)); ?></option>
    <?php endwhile; ?>
<?php endwhile;?>

But this prints

<option>TorontoOttawaMontreal</option>
<option>TorontoOttawaMontreal</option>
<option>TorontoOttawaMontreal</option>

I need it to print

<option>Toronto</option>
<option>Ottawa</option>
<option>Montreal</option>
2
  • Don't your <option> tags need a value attribute, e.g. <option value="Montreal">Montreal</option>? Commented Aug 27, 2022 at 20:24
  • What have you tried to resolve the problem? Where are you stuck? Why not gather all elements first, remove the duplicates, and print the remaining list? Commented Aug 28, 2022 at 13:09

3 Answers 3

1

Added htmlentities and strip tags for security because it seems like this data must be coming from users.

<? foreach( array_unique( $locations ) as $location ) { ?>
    <option value="<?= htmlentities( $location ) ?>"><?= strip_tags( $location ) ?></option>;
<? } ?>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use in_array() function to check if value exists in array. Try below code snippet.

<select name="field_name">
<?php
$locations = [];
while( have_posts() ) :
    the_post();
    while ( have_rows( 'location_info' ) ) :
        the_row(); 
        $loc = get_sub_field( 'location' );
        if( in_array( $loc, $locations ) ) {
            continue;
        }
        $locations[] = $loc;
        ?>
        <option value="<?php echo esc_attr( $loc ); ?>"><?php echo esc_html( $loc ); ?></option>
    <?php endwhile; ?>
<?php endwhile; ?>
<?php unset( $locations ) ?>
</select>

4 Comments

This is wrong, $locations is not defined when you do an if check, and you just echo the $location which is an array, which will bring errors.
where is the select tag ?
@VijayHardaha $locations is an array but $location(singular) is not. I've changed the name to $loc to make it less confusing.
Ok, This looks ok, I was assuming that get_sub_field( 'location' ) returns an array, but I think, it returns a string then via the loop we create an array as you're doing.
0
<?php while( have_posts() ) : the_post(); ?>
    <?php while ( have_rows( 'location_info' ) ) : the_row(); ?>
        <?php $locations[] = get_sub_field( 'location' ); ?>
    <?php endwhile; ?>
<?php endwhile;?>

<?php $locations_options = array_unique($locations); ?>
<select>
    <?php foreach( $locations_options as $location ): ?>
        <option value="<?php echo $location; ?>"><?php echo $location; ?></option>
    <?php endforeach; ?>
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.