PHP:从数组\计数\值返回顶部值

PHP:从数组\计数\值返回顶部值,php,arrays,word-frequency,Php,Arrays,Word Frequency,我的代码当前正在返回字符串中的单词列表,其中包含每个单词的出现次数。如何仅返回出现频率最高的前10个单词 //put text into a string $string = file_get_contents($path); //covert string to lowercase $string = strtolower($string); //put list of stopwords in an array $stopwords = a

我的代码当前正在返回字符串中的单词列表,其中包含每个单词的出现次数。如何仅返回出现频率最高的前10个单词

    //put text into a string
    $string = file_get_contents($path);

    //covert string to lowercase
    $string = strtolower($string);

    //put list of stopwords in an array
    $stopwords = array('a','able','about','above','abroad','according','accordingly','across','actually','adj','after','afterwards','again','against','ago','ahead','ain\'t','all','allow','allows','almost','alone','along','alongside','already','also','although','always','am','amid','amidst','among','amongst','an','and','another','any','anybody','anyhow','anyone','anything','anyway','anyways','anywhere','apart','appear','appreciate','appropriate','are','aren\'t','around','as','a\'s','aside','ask','asking','associated','at','available','away','awfully','b','back','backward','backwards','be','became','because','become','becomes','becoming','been','before','beforehand','begin','behind','being','believe','below','beside','besides','best','better','between','beyond','both','brief','but','by','c','came','can','cannot','cant','can\'t','caption','cause','causes','certain','certainly','changes','clearly','c\'mon','co','co.','com','come','comes','concerning','consequently','consider','considering','contain','containing','contains','corresponding','could','couldn\'t','course','c\'s','currently','d','dare','daren\'t','definitely','described','despite','did','didn\'t','different','directly','do','does','doesn\'t','doing','done','don\'t','down','downwards','during','e','each','edu','eg','eight','eighty','either','else','elsewhere','end','ending','enough','entirely','especially','et','etc','even','ever','evermore','every','everybody','everyone','everything','everywhere','ex','exactly','example','except','f','fairly','far','farther','few','fewer','fifth','first','five','followed','following','follows','for','forever','former','formerly','forth','forward','found','four','from','further','furthermore','g','get','gets','getting','given','gives','go','goes','going','gone','got','gotten','greetings','h','had','hadn\'t','half','happens','hardly','has','hasn\'t','have','haven\'t','having','he','he\'d','he\'ll','hello','help','hence','her','here','hereafter','hereby','herein','here\'s','hereupon','hers','herself','he\'s','hi','him','himself','his','hither','hopefully','how','howbeit','however','hundred','i','i\'d','ie','if','ignored','i\'ll','i\'m','immediate','in','inasmuch','inc','inc.','indeed','indicate','indicated','indicates','inner','inside','insofar','instead','into','inward','is','isn\'t','it','it\'d','it\'ll','its','it\'s','itself','i\'ve','j','just','k','keep','keeps','kept','know','known','knows','l','last','lately','later','latter','latterly','least','less','lest','let','let\'s','like','liked','likely','likewise','little','look','looking','looks','low','lower','ltd','m','made','mainly','make','makes','many','may','maybe','mayn\'t','me','mean','meantime','meanwhile','merely','might','mightn\'t','mine','minus','miss','more','moreover','most','mostly','mr','mrs','much','must','mustn\'t','my','myself','n','name','namely','nd','near','nearly','necessary','need','needn\'t','needs','neither','never','neverf','neverless','nevertheless','new','next','nine','ninety','no','nobody','non','none','nonetheless','noone','no-one','nor','normally','not','nothing','notwithstanding','novel','now','nowhere','o','obviously','of','off','often','oh','ok','okay','old','on','once','one','ones','one\'s','only','onto','opposite','or','other','others','otherwise','ought','oughtn\'t','our','ours','ourselves','out','outside','over','overall','own','p','particular','particularly','past','per','perhaps','placed','please','plus','possible','presumably','probably','provided','provides','q','que','quite','qv','r','rather','rd','re','really','reasonably','recent','recently','regarding','regardless','regards','relatively','respectively','right','round','s','said','same','saw','say','saying','says','second','secondly','see','seeing','seem','seemed','seeming','seems','seen','self','selves','sensible','sent','serious','seriously','seven','several','shall','shan\'t','she','she\'d','she\'ll','she\'s','should','shouldn\'t','since','six','so','some','somebody','someday','somehow','someone','something','sometime','sometimes','somewhat','somewhere','soon','sorry','specified','specify','specifying','still','sub','such','sup','sure','t','take','taken','taking','tell','tends','th','than','thank','thanks','thanx','that','that\'ll','thats','that\'s','that\'ve','the','their','theirs','them','themselves','then','thence','there','thereafter','thereby','there\'d','therefore','therein','there\'ll','there\'re','theres','there\'s','thereupon','there\'ve','these','they','they\'d','they\'ll','they\'re','they\'ve','thing','things','think','third','thirty','this','thorough','thoroughly','those','though','three','through','throughout','thru','thus','till','to','together','too','took','toward','towards','tried','tries','truly','try','trying','t\'s','twice','two','u','un','under','underneath','undoing','unfortunately','unless','unlike','unlikely','until','unto','up','upon','upwards','us','use','used','useful','uses','using','usually','v','value','various','versus','very','via','viz','vs','w','want','wants','was','wasn\'t','way','we','we\'d','welcome','well','we\'ll','went','were','we\'re','weren\'t','we\'ve','what','whatever','what\'ll','what\'s','what\'ve','when','whence','whenever','where','whereafter','whereas','whereby','wherein','where\'s','whereupon','wherever','whether','which','whichever','while','whilst','whither','who','who\'d','whoever','whole','who\'ll','whom','whomever','who\'s','whose','why','will','willing','wish','with','within','without','wonder','won\'t','would','wouldn\'t','x','y','yes','yet','you','you\'d','you\'ll','your','you\'re','yours','yourself','yourselves','you\'ve','z','zero', '+', '-', '=');

    //get each word's frequency sorted by most frequent while filtering out stopwords
    $words = str_word_count($string, 1);
    $frequency = array_count_values(array_diff($words, $stopwords));
    arsort($frequency);       

    //print each word and its frequency
    echo '<pre>';
    print_r($frequency);
    echo '</pre>';
//将文本放入字符串中
$string=file\u get\u contents($path);
//将字符串转换为小写
$string=strtolower($string);
//将停止字列表放入数组中
$stopwords=数组(‘a’、‘能’、‘关于’、‘上面’、‘国外’、‘根据’、‘相应’、‘跨越’、‘实际上’、‘调整’、‘之后’、‘之后’、‘再次’、‘反对’、‘以前’、‘前进’、‘不’、‘所有’、‘允许’、‘几乎’、‘独自’、‘一起’、‘已经’、‘还有’、‘虽然’、‘总是’、‘我’、‘中间’、‘中间’、‘中间’、‘中间’、‘任何人’、‘任何人’、‘任何人’、‘任何人’等等)ng’、‘无论如何’、‘无论如何’、‘任何地方’、‘分开’、‘出现’、‘欣赏’、‘是’、‘不是’、‘在周围’、‘作为’、‘在旁边’、‘问’、‘问’、‘联系’、‘在’、‘可用’、‘离开’、‘非常’、‘b’、‘回来’、‘向后’、‘是’、‘成为’、‘因为’、‘成为’、‘成为’、‘被’、‘之前’、‘开始’、‘在后面’、‘在下面’、‘相信’等等ide’、‘除此之外’、‘最好’、‘更好’、‘超越’、‘两者’、‘简短’、‘但是’、‘由’、‘c’、‘来’、‘能’、‘不能’、‘不能’、‘标题’、‘原因’、‘原因’、‘确定’、‘改变’、‘清楚’、‘来’、‘来’、‘关心’、‘结果’、‘考虑’、‘考虑’、‘包含’、‘包含’、‘不能’‘当然’、‘现在’、‘做’、‘做’、‘做’、‘做’、‘不做’、‘肯定’、‘描述’、‘尽管’、‘做’、‘不做’、‘不同’、‘直接’、‘做’、‘做’、‘做’、‘做’、‘做’、‘不做’、‘向下’、‘在’、‘e’、‘每个’、‘教育’、‘eg’、‘八’、‘八十’、‘要么’、‘其他’、‘其他’、‘结束’、‘足够’、‘甚至’、‘永远’、‘等等“每个人”、“每个人”、“每个人”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件事”、“每件“走了”、“走了”、“得到了”、“问候”、“h”、“有”、“没有”、“一半”、“发生了”、“几乎没有”、“有”、“没有”、“有”、“没有”、“有”、“有”、“他”、“他”、“他会”、“你好”、“帮助”、“因此”、“她”、“在这里”、“以后”、“在这里”、“在这里”、“在这里”、“在这里”、“在这里”、“在这里”、“在这里”、“在这里”、“在这里”、“她”、“她自己”、“他”、“他”、“他自己”、“他自己”、“他自己”、“如何”、“然而”、“如何”、“在这里”、“在这里”、“在这里”、“他自己”、“他自己”、“如何”等等。”“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“即时”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“如果”、“我”、“我”、“我”、“如果”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“我”、“后来的','后来的','l另外,“最少”,“最少”,“最少”,“最少”,“让”,“让我们”,“喜欢”,“喜欢”,“可能”,“同样”,“很少”,“看起来”,“看起来”,“看起来”,“低”,“低”,“低”,“低”,“制造”,“主要”,“制造”,“制造”,“多”,“可能”,“可能”,“可能”,“我”,“平均”,“同时”,“仅仅”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“不”,“我”,“可能”,“我”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“可能”,“我”,“可能”,“我”,“可能”,“可能”,“可能”,“可能”,“我”,“可能”,“可能”,“可能”,“我”,“可能”,“可能”,“我”,“更多”,“最可能”,“小姐”,“必须”,“大多数”,“先生”,“必须”,“大多数”,“大多数”,“必须”,“大多数”,“我的”等等”stn't'、'my'、'ime'、'n'、'name'、'either'、'nd'、'near'、'near'、'needs'、'needs'、'never'、'neverf'、'neverless'、'neverless'、'next'、'next'、'night'、'nobody'、'non'、'none'、'notone'、'nor'、'nor'、'nor'、'nor'、'nor'、'normally'、'not'、'not'、'nother'、'noverless'、'now'、'novely'、'now'、'now'、'novely'、'now'、'now'、'now'、'now'、'now'、'now'、'now'、n'、'oh'、'ok'、'old'、'on'、'one'、'one'、'one's'、'one's'、'only'、'other'、'other'、'others'、'others'、'others'、'should'、'ours'、'ours'、'ours'、'outs'、'outside'、'over'、'otheral'、'own'、'p'、'specific'、'pers'、'prospecified'、'placed'、'please'、'please'、'please'、'plus'、'problemble'、'provided'、'q'、'‘相当’、‘qv’、‘r’、‘r’、‘r’、‘r’、‘真的’、‘合理’、‘最近’、‘最近’、‘关于’、‘不管’、‘问候’、‘相对’、‘分别’、‘正确’、‘圆’、‘s’、‘说’、‘说’、‘说’、‘第二’、‘看’、‘看’、‘似乎’、‘看’、‘看’、‘自我’、‘自我’、‘理智’、‘认真’、‘七’、‘认真’、‘认真’、‘七个’ral'、'shall'、'shan't'、'she'、'she'd'、'she's'、'should'、'should'、'since'、'so'、'some'、'somethine'、'somethine'、'somethine'、'somethine'、'somethine'、'somethine'、'somethine'、'somethine'、'somethine'、'time'、'something'、'somethine'、'something'、'something'、'something'、'something'、'something'、'something'、'something'、'something'、's'、's'、's'、's'、's'、's'、's‘谢谢’、‘thanx’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘than’、‘than’、‘thance’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’、‘that’“三十”,“这个”,“彻底”,“那些”,“虽然”,“三”,“通过”,“通过”,“因此”,“直到”,“到”,“一起”,“太”,“采取”,“走向”,“尝试”,“真正的”,“尝试”,“尝试”,“不尝试”,“两次”,“两次”,“u”,“un”,“un”,“un”,“UNDOWN”,“UNDOWN”,“不幸的是”,“除非”,“不可能”,“不一样”,“不可能”,“直到”,“向上”,“向上”,“向上”,“向上”,“向上”,“使用”,“使用中”、“有用”、“有用”、“使用中”、“有用”、“使用中”、“使用中”、“使用中”、“使用中”、“通常”、“通常”、“v”、“价值”、“价值”、“各种”、“与”不同”、“与”比较”、“非常”、“维”这些”、“维维”、“途途经”、“使用中”、“使用中”、“使用中”、“使用中”、“使用中”、“使用中”、“使用中”、“使用中”、“使用中”、“使用中”、“使用中”、“通常”、“经常”、“v”、“价值”、“价值”、“各种”、“与”不同”、“或”、“非常”、“非常”、“维维”、“途经”、“维维”、“维维”、“维维”、“维维维”、“维维”、“维维”、“维维”、“维维维”、“维维”、“维维维维”、“维维”、“维维”、“维维”、“维维维维维”、“维维维”、“维维”、“维维维”、“维维”、“维维维维”、“维维维维维”、“维维”、“维维维维”、“维维”、“维维维维维”、“维维维”、“维维”、“维维”、“维维”、“维维维维”、“维维维维维维维维”、“维维维维w这里有“,”whereon“,”where“,”where“,”where“,”which“,”while“,”where“,”where“,”where“,”where“,”where“,”where“,”where“,”where“,”where“,”where“,”where“,”where“,”will“,”will“,”will“,”which“,”which“,”which“,”where“,”where“,”where“,”where“,”where“,”where“,”where“,”where“,”where“,”will“,”will“,”will“,”will“,”will“,”will“,”will“,”will“,”will“,”内“,”无
$output = array_slice($frequency, 0, 10);

foreach($output as $key => $word){
    echo $key;
    echo "<br>"; 
}