Page 1 of 1

array(occur most frequently)

Posted: Mon Jan 08, 2007 9:04 pm
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

Posted: Mon Jan 08, 2007 9:09 pm
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);

Posted: Mon Jan 08, 2007 9:15 pm
by horszelim
if i only want to display three strings that occur most frequently in the parameter array, how?

Thanks

Posted: Mon Jan 08, 2007 9:55 pm
by volka
with a for-loop, see http://de3.php.net/for

Posted: Mon Jan 08, 2007 10:09 pm
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.