Page 1 of 1

Checking for duplicate values

Posted: Sat Mar 03, 2007 7:35 am
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?

Posted: Sat Mar 03, 2007 7:59 am
by feyd
Store them into an array. Then use array_unique() == $array

code

Posted: Sat Mar 03, 2007 4:13 pm
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]