OK, I've done some searching on this and tried some code but haven't found anything that seems to help.

It ought to be a simple problem to solve. I guess it's also a common one.

I have an array of IDs - they're Soccer Player IDs to be specific, chosen by a user on a web form. It's possible that they might pick the same player more than once, and I'd like to report this to them as an error.

So, I know I can remove duplicates by using array_unique. And I can then compare the number of items in each array to find out if there were duplicates. But, how can I find out what the duplicate values were?

I guess I can iterate through the unique array, and count how many times each value appears in the original. It seems like a long-winded approach.

Any help would be appreciated, thanks.

function check_for_duplicates ($player_1, $player_2, $player_3, $player_4, $player_5, $player_6, $player_7, $player_8, $player_9, $player_10, $player_11) {

  //Put the Player IDs into an array
  $chosen_players = array($player_1, $player_2, $player_3, $player_4, $player_5, $player_6, $player_7, $player_8, $player_9, $player_10, $player_11);

  //Remove duplicates
  $unique_players = array_unique($chosen_players);

  //Now check if there are any duplicates
  if (count($chosen_players) > count($unique_players)) {
	return False;
  } else {
	return True;
  }

}

    You could use [man]array_count_values/man and then [man]array_filter[/man] to remove the singletons:

    <?php
    function getArrayDups($array)
    {
       $counts = array_count_values($array);
       return array_filter(
          $counts,
          create_function(
             '$val',
             'return($val > 1);'
          )
       );
    }
    
    // usage test:
    $test = array('a','b','a','c','d','c','a');
    $result = getArrayDups($test);
    if(count($result))
    {
       echo "<p>You had one or more duplicate entries:</p>\n<ul>\n";
       foreach($result as $entry => $count)
       {
          echo "<li>$entry ($count)</li>";
       }
    }
    else
    {
       echo "<p>No duplicates found.</p>";
    }
    

      Thanks. That looks reasonably elegant, especially compared to some of the ideas I've seen elsewhere.

      I'll give it a try later when I've got access to the test server.

        Bjom;10923788 wrote:

        Or try:
        array_unique()

        I used array_unique in my original code. Could you suggest how this can be used to give me a value from the original array that isn't unique?

          Oops, next time I read the original post before answering. Sorry.

          You can use array_diff_assoc and array_unique together to find the duplicates like this:

           //Put the Player IDs into an array 
            $chosen_players = array($player_1, $player_2, $player_3, $player_4, $player_5, $player_6, $player_7, $player_8, $player_9, $player_10, $player_11); 
          
            //Remove duplicates 
            $unique_players = array_unique($chosen_players); 
          
           //Retrieve duplicates
            $duplicate_players = array_diff_assoc($chosen_players, $unique_players);
            //don't allow duplicate duplicates ;)
            $duplicate_players = array_unique($duplicate_players);

            Yes, that's spot on.
            Many thanks to all who replied.

              Write a Reply...