Count the Number of Certain Elements in An Array
| From: | Alice Wei | Date: | Mon, 11 Jan 2010 21:21:33 +0000 |
| Subject: | Count the Number of Certain Elements in An Array | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-300999@lists.php.net to get a copy of this message | ||
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/