Jump to content

Count number of DIFFERENT items in array


gdfhghjdfghgfhf

Recommended Posts

$cres = mysql_query($cquery) or die(mysql_error());  
while($row = mysql_fetch_array($cres)) {  
$cfields = $row["customfields"];
echo $cfields;
}

 

this code will return a list like this

 

cat
dog
horse
cat
cat
fish

 

I want to count the number of DIFFERENT items, in this array

(in example it would be 4 different items)

 

mysql_num_rows would return 6

Link to comment
Share on other sites

$cres = mysql_query($cquery) or die(mysql_error());  
while($row = mysql_fetch_array($cres)) {  
$cfields = $row["customfields"];
echo $cfields;
}

 

this code will return a list like this

 

cat
dog
horse
cat
cat
fish

 

I want to count the number of DIFFERENT items, in this array

(in example it would be 4 different items)

 

mysql_num_rows would return 6

 

$unique_elements = count(array_unique($array));

 

It leaves the original array untouched, mind you.

Link to comment
Share on other sites

Have you tried using SELECT DISTINCT ... for your query?

 

I've actually never heard of that. :o  But he might need to use the rows for something, but he also wants to count the unique elements.  He still needs them returned by the database, probably.  So my solution works fine. xD

Link to comment
Share on other sites

So my solution works fine. xD

 

Or less than 'fine' with a very large database :)

 

But what if the unique section that he's looking for has other information on the row?  He might need the information.

 

P.S: @Thread starter: Add a unique key to something that REALLY has to be unique next time.

Link to comment
Share on other sites

But what if the unique section that he's looking for has other information on the row?  He might need the information.

 

Perhaps we should re-read the original question?  Then again, scope creep is nothing new in these forums.

Link to comment
Share on other sites

But what if the unique section that he's looking for has other information on the row?  He might need the information.

 

Perhaps we should re-read the original question?  Then again, scope creep is nothing new in these forums.

 

His first post is ambiguous.  You have NO idea what he's actually doing with the data other than "then i echo each one of the entry found".  We have two different opinions on coding, so leave it at that.  Holy crap. =/

Link to comment
Share on other sites

problem with your solution Darkwater is that no array is actually generated...

 

if including distinct in the query produces the desired result and permits any other interaction required then that would be the best solution.  If however the rest of the data set is required for other jobs then creating an array of results would be necessary...

 

(I use mysql_fetch_assoc - for the simple reason that I rarely find returning fields by a numerical index very useful..)

$cres = mysql_query($cquery) or die(mysql_error());  
while($row = mysql_fetch_assoc($cres))
{  
$cfields[] = $row["customfields"];
echo $row["customfields"];
}

$unique_records = count(array_unique($cfields));

 

yes this is what darkwater said but the array $cfields  was never actually created....

Link to comment
Share on other sites

problem with your solution Darkwater is that no array is actually generated...

 

if including distinct in the query produces the desired result and permits any other interaction required then that would be the best solution.  If however the rest of the data set is required for other jobs then creating an array of results would be necessary...

 

(I use mysql_fetch_assoc - for the simple reason that I rarely find returning fields by a numerical index very useful..)

$cres = mysql_query($cquery) or die(mysql_error());  
while($row = mysql_fetch_assoc($cres))
{  
$cfields[] = $row["customfields"];
echo $row["customfields"];
}

$unique_records = count(array_unique($cfields));

 

yes this is what darkwater said but the array $cfields  was never actually created....

 

He just wants the number of unique items.  If he wanted the array, he could just use:

$newarray = array_unique($oldarray);

$count = count($newarray);

 

But that's not what he asked for. =)  He wanted an equivalent to mysql_num_rows.

Link to comment
Share on other sites

 

He just wants the number of unique items.  If he wanted the array, he could just use:

$newarray = array_unique($oldarray);

$count = count($newarray);

 

But that's not what he asked for. =)  He wanted an equivalent to mysql_num_rows.

 

NO he wanted the number of UNIQUE records NOT just the mysql_num_rows....

 

 

So tell us - how can he get the number of unique items in the result if he is NOT going to use DISTINCT and doesn't create an array so that it can actually be interrogated for unique values????

 

PLEASE look at the code I posted once more - you'll see its correct....

Link to comment
Share on other sites

 

He just wants the number of unique items.  If he wanted the array, he could just use:

$newarray = array_unique($oldarray);

$count = count($newarray);

 

But that's not what he asked for. =)  He wanted an equivalent to mysql_num_rows.

 

NO he wanted the number of UNIQUE records NOT just the mysql_num_rows....

 

 

So tell us - how can he get the number of unique items in the result if he is NOT going to use DISTINCT and doesn't create an array so that it can actually be interrogated for unique values????

 

PLEASE look at the code I posted once more - you'll see its correct....

 

That's exactly what I did...You don't realize that the count(array_unique()) bit doesn't overwrite the array.  He can still use it in a loop after he gets the number of unique records BECAUSE it doesn't overwrite it.

Link to comment
Share on other sites

Jesus H

 

Darkness...

 

I did acknowledge your correct stipulation in you code - what your FAILED TO DO was generate an array of the database results to run array_unique on...

 

If you look at my code you may spot '[]' in there somewhere - the giveaway of creating an array...

Link to comment
Share on other sites

Jesus H

 

Darkness...

 

I did acknowledge your correct stipulation in you code - what your FAILED TO DO was generate an array of the database results to run array_unique on...

 

If you look at my code you may spot '[]' in there somewhere - the giveaway of creating an array...

 

Oh.  I thought he already had his array.  My bad.  I figured he'd know how to make an array, lol. 

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.