Home > Backend Development > PHP Tutorial > php 对问卷结果进展统计

php 对问卷结果进展统计

WBOY
Release: 2016-06-13 12:25:33
Original
788 people have browsed it

php 对问卷结果进行统计

背景:

     由于具体工作的原因,我做了一份纸质的问卷调查表,调查表的主要内容是让用户对10项要求(编号为A,B....)进行优先级排序,所以我得到的结果是好几百份类似于A>I>H>G>D....等结果。

目标:

     根据用户的排序结果对这10项要求进行量化考评,最终的结果希望是A:78,B:68,C:70.....,以找出哪一个要素相对重要,而另一些相对不重要。

做法:

     根据编号的排位名次,赋与不一样的权重,统计所有的结果,将这些权重汇总。例如:“ABCDEFGHIJ”这个结果表示A这一项得到10分,J这项得到了1分,D这项得到了7分。

知识点:

     文件读取;循环;关联数组;数组排序。

php代码:

<span style="color: #008080;"> 1</span> <span style="color: #800080;">$rs</span> =<span style="color: #0000ff;">array</span>("A"=>0,"B"=>0,"C"=>0,"D"=>0,"E"=>0,"F"=>0,"G"=>0,"H"=>0,"I"=>0,"J"=>0<span style="color: #000000;">);</span><span style="color: #008080;"> 2</span> <span style="color: #800080;">$handle</span> = <span style="color: #008080;">fopen</span>('./file.txt', 'r'<span style="color: #000000;">);</span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">while</span>(!<span style="color: #008080;">feof</span>(<span style="color: #800080;">$handle</span><span style="color: #000000;">))</span><span style="color: #008080;"> 4</span> <span style="color: #000000;">{</span><span style="color: #008080;"> 5</span>     <span style="color: #800080;">$string</span> = <span style="color: #008080;">fgets</span>(<span style="color: #800080;">$handle</span>, 1024<span style="color: #000000;">);</span><span style="color: #008080;"> 6</span>     <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>strlen(<span style="color: #800080;">$string</span>);<span style="color: #800080;">$i</span>++<span style="color: #000000;">)</span><span style="color: #008080;"> 7</span> <span style="color: #000000;">    {</span><span style="color: #008080;"> 8</span>         <span style="color: #800080;">$t</span> = <span style="color: #008080;">strtoupper</span>(<span style="color: #800080;">$string</span>[<span style="color: #800080;">$i</span><span style="color: #000000;">]);</span><span style="color: #008080;"> 9</span>         <span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$rs</span>[<span style="color: #800080;">$t</span><span style="color: #000000;">]))</span><span style="color: #008080;">10</span>             <span style="color: #800080;">$rs</span>[<span style="color: #800080;">$t</span>] = <span style="color: #800080;">$rs</span>[<span style="color: #800080;">$t</span>]+ <span style="color: #008080;">strlen</span>(<span style="color: #800080;">$string</span>) - <span style="color: #800080;">$i</span><span style="color: #000000;">;</span><span style="color: #008080;">11</span> <span style="color: #000000;">    }</span><span style="color: #008080;">12</span> <span style="color: #000000;">}</span><span style="color: #008080;">13</span> <span style="color: #008080;">fclose</span>(<span style="color: #800080;">$handle</span><span style="color: #000000;">);</span><span style="color: #008080;">14</span> <span style="color: #008080;">arsort</span>(<span style="color: #800080;">$rs</span><span style="color: #000000;">);</span><span style="color: #008080;">15</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$rs</span>);
Copy after login

说明:file.txt是一个文本文件,其中的每一行表示一份问卷的结果,类似于“ABCDEFGHIJ”这样的东西。这个文件是如何得到了呢?好吧,我承认不是我自己手动录入的,我找了些人帮忙(为什么不做网上问卷呢?省得这么麻烦)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template