Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've created a filter for my search results that allows people to filter user searches based on their State from their profile. The problem I am having is that there are duplicate states showing in the drop-down list for users that have the same state.

Here is the current code trying to eliminate that problem.

<div class="state">
    <input type="hidden" name="search_type" value="members">
    <select id="stateDrop" name="state">
        <option value="name">State</option>
        <?php
            $prevState = ""; 
            foreach($users as $user) {
                $state = get_cimyFieldValue($user->ID, 'STATE'); // Grabing their state from their profile page

                if($prevState != $state) {
                    $prevState = $state;
                    echo '<option value="'.$state.'">'.$state.'</option>';
                }
            }
        ?>
    </select>
</div>

This works great when 2 states are right after each other, but when I have something like the below, it will not eliminate the duplicates...

Not working
  -California
  -Washington
  -California

Working
  -California
  -California
  -Washington

What can I do differently to get the results I need and possibly sort it alphabetically? Thanks,

share|improve this question

closed as off topic by anu, Michael, toscho May 21 '13 at 15:38

Questions on WordPress Development Stack Exchange are expected to relate to WordPress within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer 1

up vote 2 down vote accepted

Use array_unique()

<?php
        foreach($users as $user) {
            $states[] = get_cimyFieldValue($user->ID, 'STATE'); // Grabing their state from their profile page
        }
        $states = array_unique($states);
    ?>
    <div class="state">
        <input type="hidden" name="search_type" value="members">
        <select id="stateDrop" name="state">
            <option value="name">State</option>
            <?php
            foreach($states as $state) {
                echo '<option value="'.$state.'">'.$state.'</option>';
            }
            ?>
        </select>
    </div>
share|improve this answer
    
Awesome! This is what I was looking for thanks! –  Romes May 21 '13 at 15:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.