PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Wed Sep 10, 2003 3:06 am
Arrays are not really my forte, so i'm calling on a little help.
I have an Array called $who_sorted.
This Array could contain something like:
Mark
Mark
Mark
Claire
Claire
Sean
Claire
Mark
Sean
Sean
Sean
Now, what i need to do is count how many times each name occurs in the Array bearing in mind i don't know what names are going to be in it.
Any ideas?
Thanks
Mark
delorian
Forum Contributor
Posts: 223 Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland
Post
by delorian » Wed Sep 10, 2003 3:29 am
Code: Select all
sort($array); // your array should be sorted first
$tmp = null; // actual name
$count = array(); // how many
foreach($array as $val) {
if($tmp!=$val) $tmp=$val; // actual counting name
$count[$tmp]++; // count the name
}
That should do the trick, I think.
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Wed Sep 10, 2003 3:51 am
Mate, that worked a treat
Thanks
Mark
twigletmac
Her Royal Site Adminness
Posts: 5371 Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK
Post
by twigletmac » Wed Sep 10, 2003 4:30 am
JayBird
Admin
Posts: 4524 Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:
Post
by JayBird » Wed Sep 10, 2003 4:59 am
ah yes Mac, that seem like an easier solution.
Thanks guys and gals
delorian
Forum Contributor
Posts: 223 Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland
Post
by delorian » Fri Sep 12, 2003 12:41 pm
Yes, much easier
- but it took me less time to write it than find the allready opened door.