|
Posted by Rik on 08/15/06 20:20
Karl Groves wrote:
> "Rik" <luiheidsgoeroe@hotmail.com> wrote in
> news:dc00a$44e22551$8259c69c $32576@news1.tudelft.nl:
>
>> Karl Groves wrote:
>>>
>>> $top = array_keys(array_slice(rsort(array_count_values($words)),0,
>>> $number,true));
>>>
>>> print("<pre>");
>>> var_dump($top);
>>> print("</pre>");
>>>
>>>
>>> Returns the following error:
>>>
>>> Warning: array_keys() [function.array-keys]: The first argument
>>> should be an array in /path/to/file.php on line 7
>>
>> Je m'excuse. rsort offcourse returns a bool.
>>
>> $occurence = array_count_values($words);
>> rsort($occurence);
>> $top = array_slice(array_keys($occurence),0,$number);
>>
>> Suits me right for trying to make an illegible oneliner.
>>
>
>
> Thanks, though this is now only giving the keys, not the words
> themselves
>
> array(3) {
> [0]=>
> int(0)
> [1]=>
> int(1)
> [2]=>
> int(2)
> }
Pfff, never a good idea to try to post while talking to your mother on the
phone (oh yeah, which disease has this family member got now...). You have
my complete and undivided attention now: occording to the manual rsort()
deletes the keys we so desperately want. arsort() is the solution.
Here we go, and I've finally tested the code before posting :-).
<?php
$number = 4;
$words = array("foo", "lorem", "ipsum", "foo", "dolor", "sit", "bar",
"amet", "four", "score", "foo", "and", "seven", "bar", "years", "ago",
"foo", "our", "lipsum", "forefathers", "foo");
$occurence = array_count_values($words);
arsort($occurence);
$top = array_slice(array_keys($occurence),0,$number);
print_r($top);
?>
(
[0] => foo
[1] => bar
[2] => years
[3] => seven
)
Sorry for the mess I posted earlier.
Grtz,
--
Rik Wasmus
Navigation:
[Reply to this message]
|