Jump to content

[SOLVED] can't get this array to work


j4ymf

Recommended Posts

hello all

 

im trying to get an array to work but i just cant do it.

my aim is, im running a fantasy football site and i don't want people to be able to add the same player in twice.

 

how would i go about doing it?

this is what i have and it dosen't work

 

many thanks j4ymf

 

 


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

  //Put the players 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);
  
  //Compare the before and after to get any duplicates out
  $duplicate_players = array_diff($chosen_players, $unique_players);
  
  //Now check if there are any duplicates
  if (count($duplicate_players) > 0) {
global $bad_player;
$bad_player = $duplicate_players[0]; // only returns the first duplicate, but could be modified to return then all
return False;
  } else {
return True;
  }

}

Link to comment
Share on other sites

Sorry i will try to explain

 

well im trying to check if the team has any duplicates,

 

so i have 11 players being entered,

but at the moment it lets the team through when you are entering it, even if i have two players the same.  so im trying to avoid terry being put in twice!!!

 

does that make sence

 

so im after the code to check if it has any duplicates in it,

 

thanks j4ymf

 

jason

Link to comment
Share on other sites

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

//Put the players 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);

//Compare the before and after to get any duplicates out
$duplicate_players = array_diff_assoc($chosen_players, $unique_players);

//Now check if there are any duplicates
if (count($duplicate_players) > 0) {
	global $bad_player;
	$bad_player = array_shift($duplicate_players); // only returns the first duplicate, but could be modified to return then all
	return False;
} else {
	return True;
}
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.