array(occur most frequently)

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

Locked
horszelim
Forum Newbie
Posts: 7
Joined: Mon Jan 08, 2007 7:28 pm

array(occur most frequently)

Post by horszelim »

Hello.

May i have an idea to solve this question?

Parameter: An array of strings
Return value: A list of three strings that occur most frequently in the parameter array

I need to use sorting?

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

try

Code: Select all

<?php
$arr = array(
	'z', 'b', 'l', 'd',
	'a', 'x', 'c', 'd',
	'a', 'y', 'z', 'd',
	'x', 'b', 'c', 'l',
	'a', 'b', 'c', 'l',
	'a', 'b', 'c', 'l',
	'a', 'h', 'g', 'd',
	'a', 'b', 'c', 'd',
	'a', 't', 'c', 'i');
	

$c = array_count_values($arr);
arsort($c);
print_r($c);
horszelim
Forum Newbie
Posts: 7
Joined: Mon Jan 08, 2007 7:28 pm

Post by horszelim »

if i only want to display three strings that occur most frequently in the parameter array, how?

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

with a for-loop, see http://de3.php.net/for
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

There's already a thread with the very same subject by you. Locked.
[url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url] Section 1.1 wrote:3. Do not make multiple, identical posts. This is viewed as spam and will be deleted.
Locked