Compare array with unique array...
PHP Code:
$values = array(3, 5, ... );
$set_contains_dupes = $values === array_unique($values);
This code I wrote last year might help you with some other automation, but here's what I used to validate a submitted team (quite complex rules) :
PHP Code:
/**
* Checks a submitted team (create/edit) for validity.
*
* @return mixed true on success; string on failure (error msg)
*/
protected function verifySubmittedTeam()
{
$variablePositions = array('batsman', 'bowler');
$data = array();
$totalPlayers = array();
$wicketkeepers = $this->countWicketKeepers();
foreach ($this->Player->positions as $identifier => $bit)
{
####################################################################
// Main Team
$data[$identifier] = $this->vb->input->clean_gpc(
'p',
$identifier,
TYPE_ARRAY_UINT
);
// Removing Invalid/Empty Players
foreach ($data[$identifier] as $index => $playerid)
{
$name = $this->availablePlayers[$playerid]['name'];
if (!$playerid)
{
unset($data[$identifier][$index]);
}
else if (empty($this->availablePlayers[$playerid]))
{
return "$name is not an available player for this game.";
}
else if (!($this->availablePlayers[$playerid]['positions'] & $bit))
{
return "$name is not an available $identifier for this game.";
}
$totalPlayers[$playerid]++;
}
$count = count($data[$identifier]);
$required = $this->Team->slots[1][$identifier];
// Check player counts for static positions
if (
$count != $required and (
$this->game['type'] == 2 or (
in_array($this->game['type'], array(1, 3)) and
!in_array($identifier, $variablePositions)
)
)
)
{
$title = $this->Player->positionText[$identifier];
return "You have selected $count players for the $title position. A valid team
must contain exactly $required.";
}
$data['special'][$identifier] = $this->vb->input->clean_gpc(
'p',
"special_$identifier",
TYPE_ARRAY_UINT
);
####################################################################
// Reserves
$data["r_$identifier"] = $this->vb->input->clean_gpc(
'p',
"r_$identifier",
TYPE_ARRAY_UINT
);
// Removing Invalid/Empty Players
foreach ($data["r_$identifier"] as $index => $playerid)
{
$name = $this->availablePlayers[$playerid]['name'];
if (!$playerid and !($identifier == 'wicketkeeper' and $wicketkeepers == 2 and $index == 1))
{
unset($data["r_$identifier"][$index]);
return "Please select a player for reserve $identifier # " . ($index + 1) . ".";
}
if ($playerid and empty($this->availablePlayers[$playerid]))
{
return "$name is not an available player for this game.";
}
// Error on Unavailable Position
if ($playerid and !($this->availablePlayers[$playerid]['positions'] & $bit))
{
return "$name is not an available $identifier for this game.";
}
$totalPlayers[$playerid]++;
}
// Incorrect # of Players
if (($count = count($data["r_$identifier"])) != ($required = $this->Team->slots[1]["r_$identifier"]))
{
if (!(
$identifier == 'wicketkeeper' and
$wicketkeepers == 2 and
$required == 2 and
$count == 1
))
{
$title = 'Reserve ' . $this->Player->positionText[substr($identifier, 2)];
return "You have selected $count players for the $identifier position. A valid team
must contain exactly $required.";
}
}
}
// Check variable ODI positions
if ($this->game['type'] == 1 or $this->game['type'] == 3)
{
$numBatsmen = count($data['batsman']);
$numBowlers = count($data['bowler']);
if ($numBatsmen == 5)
{
if ($numBowlers != 3)
{
return 'With 5 batsmen, you must have <strong>3</strong> bowlers.';
}
}
else if ($numBatsmen == 4)
{
if ($numBowlers != 4)
{
return 'With 4 batsmen, you must have <strong>4</strong> bowlers.';
}
}
else
{
return 'You must have either 4 or 5 batsmen for your team to be valid.';
}
}
foreach ($totalPlayers as $playerid => $num)
{
if ($playerid and $num > 1)
{
$name = $this->availablePlayers[$playerid]['name'];
return "You can only use each player once; $name is in $num positions.";
}
}
$bonus = array();
foreach ($data['special'] as $identifier => $players)
{
foreach ($players as $num => $type)
{
if ($type)
{
if (strpos($identifier, 'r_') !== false)
{
return 'You may only select bonuses for main players.';
}
if (empty($data[$identifier][$num]))
{
continue;
}
$bonus[$data[$identifier][$num]] = $type;
}
}
}
$this->bonusPlayers = $bonus;
unset($data['special'], $bonus);
$expected = array(1, 1, 1, 1, 2);
$sorted = array_values($this->bonusPlayers);
sort($sorted);
if ($sorted !== $expected)
{
$assoc = array(1 => 'bonus players', 2 => 'captains');
$selected = array_count_values($sorted) + array(1 => 0, 2 => 0);
return "You must pick 4 bonus players and one captain; you have
$selected[1] $assoc[1] and $selected[2] $assoc[2].";
}
$this->submittedTeam = $data;
return true;
}