Re: array_unique() and count issue

From: Date: Mon, 20 Aug 2001 02:09:22 +0000
Subject: Re: array_unique() and count issue
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-63441@lists.php.net to get a copy of this message
On Sat, 18 Aug 2001 17:27, CGI GUY wrote: > OK, I want to count the number of unique elements in > an array, then print the total. However, both methods > I've tried return "1"; the man pages explain that the > count() function will return 1 if the var is set but > not an array. But if you look at the 2nd method esp., > I've set an if-else !is_set() alert which doesn't > appear... Either way, it's a 1. Help! > > Method 1: > > <? > > $count = > array_unique(mysql_fetch_array($mysql_result)); > > $total = count($count); > > print($total); > > ?> mysql_fetch_array only grabs the content of one row returned from a DB query, and puts it in an array - so your array here only has one element. > Method 2: > > <? > > $array = mysql_fetch_array($mysql_result); > > $newarray = array_values($array); > > if (!is_array($newarray)) { > print("Not an array"); > > } else { > > $count = array_unique($newarray); > > foreach($count as $key => $value) { > $count[$key] = $value++; > } > > print($value); > > > } > ?> As above. You need to cycle through the results from the DB and add them all to the array. But having said that, it seems to me that what you are really trying to do is to return unique entries from the database? In this case, look at using the DISTINCT keyword in your SQL query. -- David Robley Techno-JoaT, Web Maintainer, Mail List Admin, etc CENTRE FOR INJURY STUDIES Flinders University, SOUTH AUSTRALIA Breathing may be hazardous to your health.

« previous php.general (#63441) next »