Checking for duplicate values

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
keveh
Forum Commoner
Posts: 27
Joined: Mon Aug 08, 2005 5:50 am

Checking for duplicate values

Post by keveh »

I have 4 values that are being submitted from a form, but I need to check if all these values are unique.

Say these are our four values:

Code: Select all

$battle1 = $_POST['battle1'];
$battle2 = $_POST['battle2'];
$battle3 = $_POST['battle3'];
$battle4 = $_POST['battle4'];
Which is the best way to go about checking no two values are the same?

I was thinking about putting the values in an array and then checking each value doesn't appear twice, I'm just not sure how to approach it.

Could anybody recommend anything?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Store them into an array. Then use array_unique() == $array
dsdintn
Forum Newbie
Posts: 7
Joined: Sat Mar 03, 2007 2:27 pm

code

Post by dsdintn »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Code: Select all

$battle1 = $_POST['battle1']; 
$battle2 = $_POST['battle2']; 
$battle3 = $_POST['battle3']; 
$battle4 = $_POST['battle4'];

    $battle_array             = array($battle1,$battle2,$battle3,$battle4);
    $battle_array_unique = array_unique($battle_array);

    $num_unique_vals = count($battle_array_unique);

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply