RE: [PHP] Count the Number of Certain Elements in An Array

From: Date: Mon, 11 Jan 2010 21:40:53 +0000
Subject: RE: [PHP] Count the Number of Certain Elements in An Array
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-301001@lists.php.net to get a copy of this message
> Date: Mon, 11 Jan 2010 18:31:43 -0300 > Subject: Re: [PHP] Count the Number of Certain Elements in An Array > From: tapicer@gmail.com > To: ajwei@alumni.iu.edu > CC: php-general@lists.php.net > > On Mon, Jan 11, 2010 at 6:21 PM, Alice Wei <ajwei@alumni.iu.edu> wrote: > > > > Hi, > > > > This seems like a pretty simple problem, but I can't seem to be able to figure it > > out. I have a lot of elements in an array, and some of them are duplicates, but I don't want to > > delete them because I have other purposes for it. Is it possible for me to find out the number of > > certain elements in an array? > > > > For example, > > > > // Create a simple array. > > $array = array(1, 2, 3, 4, 5, 3,3,4,2); > > $total = count($array); > > echo $total; > > > > If I run the code, the value of $total would be 9. However, what I really want to do is > > to get the values of the different instances of each, like: > > > > 1 => 1 > > 2 => 2 > > 3 => 3 > > 4=> 2 > > 5=> 1 > > > > Is there a simple code that I use to find this out? > > > > Thanks for your help. > > > > Alice > > > > _________________________________________________________________ > > Hotmail: Trusted email with powerful SPAM protection. > > http://clk.atdmt.com/GBL/go/196390707/direct/01/ > > > Hi, > > Try the function array_count_values. > Thanks, this is cool. There is already a function for it! > Regards, > > Jonathan _________________________________________________________________ Hotmail: Free, trusted and rich email service. http://clk.atdmt.com/GBL/go/196390708/direct/01/

« previous php.general (#301001) next »