You are here: Re: Keyword array « PHP Programming Language « IT news, forums, messages
Re: Keyword array

Posted by Rik on 08/15/06 19:51

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.

Grtz,
--
Rik Wasmus

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация