Re: Count the Number of Certain Elements in An Array

From: Date: Mon, 11 Jan 2010 21:31:43 +0000
Subject: Re: Count the Number of Certain Elements in An Array
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-301000@lists.php.net to get a copy of this message
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. Regards, Jonathan

« previous php.general (#301000) next »