Count the Number of Certain Elements in An Array
am 11.01.2010 22:21:33 von Alice Wei
--_5dbdfec6-07eb-4fb2-811e-311ad519e0b4_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
Hi,
This seems like a pretty simple problem=2C but I can't seem to be able to=
figure it out. I have a lot of elements in an array=2C and some of them ar=
e duplicates=2C but I don't want to delete them because I have other purpos=
es for it. Is it possible for me to find out the number of certain elements=
in an array?=20
For example,
// Create a simple array.
$array =3D array(1=2C 2=2C 3=2C 4=2C 5=2C 3=2C3=2C4=2C2)=3B
$total =3D count($array)=3B
echo $total=3B
=20
If I run the code=2C the value of $total would be 9. However=2C what I re=
ally want to do is to get the values of the different instances of each=2C =
like:
1 =3D> 1
2 =3D> 2
3 =3D> 3
4=3D> 2
5=3D> 1
Is there a simple code that I use to find this out?=20
Thanks for your help.=20
Alice
=20
____________________________________________________________ _____
Hotmail: Trusted email with powerful SPAM protection.
http://clk.atdmt.com/GBL/go/196390707/direct/01/=
--_5dbdfec6-07eb-4fb2-811e-311ad519e0b4_--
Re: Count the Number of Certain Elements in An Array
am 11.01.2010 22:31:43 von Jonathan Tapicer
On Mon, Jan 11, 2010 at 6:21 PM, Alice Wei wrote:
>
> Hi,
>
> =A0This seems like a pretty simple problem, but I can't seem to be able t=
o 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?
>
> =A0For example,
>
> =A0 // Create a simple array.
> =A0 $array =3D array(1, 2, 3, 4, 5, 3,3,4,2);
> =A0 $total =3D count($array);
> =A0 echo $total;
>
> =A0If I run the code, the value of $total would be 9. However, what I rea=
lly want to do is to get the values of the different instances of each, lik=
e:
>
> =A0 1 =3D> 1
> =A0 2 =3D> 2
> =A0 3 =3D> 3
> =A0 4=3D> =A02
> =A0 5=3D> =A01
>
> 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: Count the Number of Certain Elements in An Array
am 11.01.2010 22:40:53 von Alice Wei
--_ab2b583c-d9ae-4465-996d-17b86be6753f_
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable
> Date: Mon=2C 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
>=20
> On Mon=2C Jan 11=2C 2010 at 6:21 PM=2C Alice Wei wr=
ote:
> >
> > Hi=2C
> >
> > This seems like a pretty simple problem=2C but I can't seem to be able=
to figure it out. I have a lot of elements in an array=2C and some of them=
are duplicates=2C but I don't want to delete them because I have other pur=
poses for it. Is it possible for me to find out the number of certain eleme=
nts in an array?
> >
> > For example=2C
> >
> > // Create a simple array.
> > $array =3D array(1=2C 2=2C 3=2C 4=2C 5=2C 3=2C3=2C4=2C2)=3B
> > $total =3D count($array)=3B
> > echo $total=3B
> >
> > If I run the code=2C the value of $total would be 9. However=2C what I=
really want to do is to get the values of the different instances of each=
=2C like:
> >
> > 1 =3D> 1
> > 2 =3D> 2
> > 3 =3D> 3
> > 4=3D> 2
> > 5=3D> 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/
>=20
>=20
> Hi=2C
>=20
> Try the function array_count_values.
>=20
Thanks=2C this is cool. There is already a function for it!
> Regards=2C
>=20
> Jonathan
=20
____________________________________________________________ _____
Hotmail: Free=2C trusted and rich email service.
http://clk.atdmt.com/GBL/go/196390708/direct/01/=
--_ab2b583c-d9ae-4465-996d-17b86be6753f_--