View Full Version : Finding Duplicate Numbers

01-08-2006, 05:05 AM

I'm looking for a solution that will determine if I have duplicate age_id's I'm pulling from a mysql database.


I'm pulling the field 'age_id' from different user records in the mysql database as shown above and I would like to determine if any of the above age_id's are the same. I dont need to know how many duplicates, only if their are any duplicates and if so I will echo a duplicate id image.

any help would be appreciated, Thanks, Jen.

ralph l mayo
01-08-2006, 05:26 AM
Inline method, if you only have to do this once:

$temp = array();
// $max is the number of variables in the form $user#->age_id to check
for ($ii = 1; $ii <= $max; ++$ii)
if (!in_array(${'user'.$ii}->age_id, $temp))
array_push($temp, $ii);
echo 'duplicate';

function friendly method:

function checkDupAgeId($ids)
return (array_unique($ids) != $ids);
checkDupAgeId(array($user1->age_id, $user2->age_id, $user3->age_id, $user4->age_id));
// true means duplicate

Neither seem really ideal, so I'm probably missing something.

01-08-2006, 06:38 AM
thanks ralph, your in-line method is working but I beleive I have other problems in my app. thanks for the help.

ralph l mayo
01-08-2006, 07:03 AM
edit: nm then :]

01-08-2006, 09:00 PM
Ralph, why do you use '$ii' ? I can't get it. I mean we use '$i' because it stands for a integer variable. What is 'ii' for? Lol. And why are you using break?

for($i=1,$used=array(); $i<=$max; $i++) {
if(in_array(${'user'.$i}->age_id, $used)) {
$duplicate_image = true;
} else { $used[]=${'user'.$i}->age_id; }

Thats a little smaller and compact, and wherever you need to do the image you just check if $duplicate_image is true, and if so, echo the image.

ralph l mayo
01-08-2006, 10:04 PM
Ralph, why do you use '$ii' ? I can't get it. I mean we use '$i' because it stands for a integer variable. What is 'ii' for? Lol. And why are you using break?

i doesn't stand for integer at all, it's just the first in the series of iterator values (i, j, k) that's been around since forever. I use ii because in a lot of fonts i looks just like l and/or 1 and doubling it makes it easier to see what's happening. This is less a problem in PHP because of the $, but it's the way I do it in every language

I use break because the number of duplicates isn't important here, only the existence of at least one. After one is found it's a waste of time and processor power to continue looping over results that don't matter.

01-08-2006, 10:07 PM
That all pretty much makes sense, but according to me, and all my partners, we all use i because it makes more sense and in more logical standards for human comprehension, it does stand for integer, which is why when everyone uses numbers they usually just name it $i.

01-09-2006, 12:35 AM
Iit's not any more logical or less if you use a variable which name is not $i as an iterator.
anyway if you don't like to go into fuzzy logic (http://mathworld.wolfram.com/FuzzyLogic.html) there's nothing that is more or less logic.

01-09-2006, 03:22 AM
Using the letter i as the variable name within a loop is not because it stands for integer. It is likely however passed on from the FORTRAN language in which the letters i-n designate integer variables. Since FORTRAN was one of the ancestors for modern day languages and programmers often have to rewrite older FORTRAN programs using modern day languages the habit of using i,j,k...n as integer variables in todays programming got carried over.

So let's end this argument and get back on the original topic at hand.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum