|
Posted by Karl Groves on 08/15/06 20:02
"Rik" <luiheidsgoeroe@hotmail.com> wrote in news:dc00a$44e22551$8259c69c
$32576@news1.tudelft.nl:
> Karl Groves wrote:
>> "Rik" <luiheidsgoeroe@hotmail.com> wrote in
>> news:ebspir$a83$1@netlx020.civ.utwente.nl:
>>
>>> Karl Groves wrote:
>>>> I'm looking to make a keyword array.
>>>>
>>>> Let's say I have the following array:
>>>>
>>>> $words = array
>>>>
>>>
("foo","lorem","foo","ipsum","bar","foo","dolor","sit","foo","bar","am
>>> e t","
>>>> lorem");
>>>>
>>>> I'd like to create a new array with the top X words, ordered by
>>>> occurrence In the above example, the new array would be:
>>>> $keywords = array("foo","bar","lorem");
>>>
>>>
>>> I assum the top X words, ordered _descending_?
>>>
>>> $number = 10;//or another if you wish
>>> $top =
>>> array_slice(array_keys(rsort(array_count_values($words))),0,
$number);
>>>
>>> If PHP5 or higher this will be better:
>>> $top =
>>> array_keys(array_slice(rsort(array_count_values($words)),0,
>>> $number,true ));
>>>
>>> Grtz,
>>
>> Thanks for the response.
>>
>> Here's what I have:
>>
>> $words = array("foo", "lorem", "ipsum", "foo", "dolor", "sit", "bar",
>> "amet", "four", "score", "foo", "and", "seven", "bar", "years",
"ago",
>> "foo", "our", "lipsum", "forefathers", "foo");
>>
>> $number = 3;
>>
>> $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)
}
--
Karl Groves
www.karlcore.com
[Back to original message]
|