背景:
由于具体工作的原因,我做了一份纸质的问卷调查表,调查表的主要内容是让用户对10项要求(编号为A,B....)进行优先级排序,所以我得到的结果是好几百份类似于A>I>H>G>D....等结果。
目标:
根据用户的排序结果对这10项要求进行量化考评,最终的结果希望是A:78,B:68,C:70.....,以找出哪一个要素相对重要,而另一些相对不重要。
做法:
根据编号的排位名次,赋与不一样的权重,统计所有的结果,将这些权重汇总。例如:“ABCDEFGHIJ”这个结果表示A这一项得到10分,J这项得到了1分,D这项得到了7分。
知识点:
文件读取;循环;关联数组;数组排序。
php代码:
<span> 1</span> <span>$rs</span> =<span>array</span>("A"=>0,"B"=>0,"C"=>0,"D"=>0,"E"=>0,"F"=>0,"G"=>0,"H"=>0,"I"=>0,"J"=>0<span>); </span><span> 2</span> <span>$handle</span> = <span>fopen</span>('./file.txt', 'r'<span>); </span><span> 3</span> <span>while</span>(!<span>feof</span>(<span>$handle</span><span>)) </span><span> 4</span> <span>{ </span><span> 5</span> <span>$string</span> = <span>fgets</span>(<span>$handle</span>, 1024<span>); </span><span> 6</span> <span>for</span>(<span>$i</span>=0;<span>$i</span><<span>strlen</span>(<span>$string</span>);<span>$i</span>++<span>) </span><span> 7</span> <span> { </span><span> 8</span> <span>$t</span> = <span>strtoupper</span>(<span>$string</span>[<span>$i</span><span>]); </span><span> 9</span> <span>if</span>(<span>isset</span>(<span>$rs</span>[<span>$t</span><span>])) </span><span>10</span> <span>$rs</span>[<span>$t</span>] = <span>$rs</span>[<span>$t</span>]+ <span>strlen</span>(<span>$string</span>) - <span>$i</span><span>; </span><span>11</span> <span> } </span><span>12</span> <span>} </span><span>13</span> <span>fclose</span>(<span>$handle</span><span>); </span><span>14</span> <span>arsort</span>(<span>$rs</span><span>); </span><span>15</span> <span>var_dump</span>(<span>$rs</span>);
说明:file.txt是一个文本文件,其中的每一行表示一份问卷的结果,类似于“ABCDEFGHIJ”这样的东西。这个文件是如何得到了呢?好吧,我承认不是我自己手动录入的,我找了些人帮忙(为什么不做网上问卷呢?省得这么麻烦)