Re: Count the Number of Certain Elements in An Array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Jan 11, 2010 at 6:21 PM, Alice Wei <ajwei@xxxxxxxxxxxxx> 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

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux