• Hello,

    i have to sort the users by city. but i have to show only unique cities (no duplication)

    i created this code which sort and display me list of the cities, but i have to get rid of the duplicated

    <?php
    
    $args  = array(
      'fields' => 'all',
      'role' => 'Subscriber',
      'meta_query' => array(
      					array(
    						'key'=>'city'
    						))
      );
    
    $blogusers = get_users($args);
    
    //custom function for comparing the data we want to sort by
    function cmp($a, $b){
      if ($a->city == $b->city) {
        return 0;
      }
      return ($a->city > $b->city) ? 1 : -1;
    
    } 
    
    usort($blogusers, 'cmp'); 
    
    foreach ($blogusers as $user ) { // get all the user's data
    
    $city = $user->city;
    
    print '<h2>' . $city . '</h2>';
    
    }
    
    ?>

    the result:

    Atlanta
    Atlanta
    Atlanta
    Boston
    Chicago
    Detroit
    New York
    San Jose
    Washington DC
    Washington DC
    ————–

    what i need is:

    Atlanta
    Boston
    Chicago
    Detroit
    New York
    San Jose
    Washington DC

    ———–

    please help

    thanks

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Sort users by unique sities’ is closed to new replies.