[PHP] Count the Number of Certain Elements in An Array
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/
Re: [PHP] Count the Number of Certain Elements in An Array
On Mon, Jan 11, 2010 at 6:21 PM, Alice Wei aj...@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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Count the Number of Certain Elements in An Array
Date: Mon, 11 Jan 2010 18:31:43 -0300 Subject: Re: [PHP] Count the Number of Certain Elements in An Array From: tapi...@gmail.com To: aj...@alumni.iu.edu CC: php-general@lists.php.net On Mon, Jan 11, 2010 at 6:21 PM, Alice Wei aj...@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/