开发者社区> 问答> 正文

php如何在二维数组中,再计算单列的最值。

在做一个统计网页,现在遇到这样一个问题。
如标题:怎么在二维数组中,找到每个单列的最值。

如图,我需要找到需要的单列的最值,然后应用相应的样式。

现在的显示效果
screenshot

相应的颜色css类我已经写好了,只需要引用就行了。
不用js代码,也就是说直接在生成的时候直接判断,再引用样式。

部分代码如下:

    $index=1;
    while($row = mysql_fetch_array($result)){
        echo '<tr>';
        echo    '<td class="center essential">'.$index.'</td>';
        echo    '<td class="essential"><a class= "user" href=search.php?qq='.$row[qq].'&user='.$row[user].'>'.$row[user].'</a></td>';
        echo    '<td class="essential">'.$row[speed].'</td>';
        echo    '<td class="essential">'.$row[keystroke].'</td>';
        echo    '<td class="essential">'.$row[length].'</td>';
        echo    '<td class="essential">'.$row[backspace].'</td>';
        echo    '<td>'.$row[wrong].'</td>';
        echo    '<td class="center">'.$row[ime].'</td>';
        echo    '<td class="center">'.$row[wordcount].'</td>';
        echo    '<td class="center">'.$row[timecost].'</td>';
        echo    '<td>'.$row[qq].'</td>';
        echo    '<td class="center">'.date("H:i",strtotime($row[timestart])).'</td>';
        echo    '<td class="center">'.$row[repeat].'</td>';
        echo    '<td>'.$row[score].'</td>';
        echo    '<td>'.$row[scoresum].'</td>';
        echo '</tr>';
        $index++;
      /************************************************
        其中的essential和center没什么其它作用,可以忽略。
       ************************************************/
    }

切成数组什么的好说,我只是不知道存入数组后如何才能进行判断。
求大神说的细点,小白在此谢过。

展开
收起
杨冬芳 2016-06-17 17:16:43 3397 0
1 条回答
写回答
取消 提交回答
  • IT从业

    知道怎么弄了, 先从数据库把每列的数据先读取出来,然后用max(),min()计算每列的最值
    然后,在输出的时候,当前列的数据是否等于这一列的最值,等于的话就给予特殊样式。
    其中需要注意的一项是:mysql_fetch_array只能取一次数据,取完一次之后里面就没有数据了。所以先把查询到的数据转存。

    部分代码如下:

        //转存查询的结果
        $temp_result = array();
        while($row=mysql_fetch_array($result)){
            array_push($temp_result, $row);
        }
        //获取每个列的所有数据,算出最值;
        $speed = array();    $keystroke = array();    $length = array();    $backspace = array(); $repeat = array();
        foreach ($temp_result as $row ) {
            array_push($speed, $row[speed]);
            array_push($keystroke, $row[keystroke]);
            array_push($length, $row[length]);
            array_push($backspace, $row[backspace]);
            array_push($repeat, $row[repeat]);
        }
        $max_speed=max($speed);
        $max_keystroke=max($keystroke);
        $min_length = min($length);
        $min_backspace = min($backspace);
        $max_repeat = max($repeat);
    
        // 输出列表
        $index=1;
        foreach ($temp_result as $row ) {
            echo '<tr>';
            echo    '<td class="center essential">'.$index.'</td>';
            echo    '<td class="essential"><a class= "user" href=search.php?qq='.$row[qq].'&user='.$row[user].'>'.$row[user].'</a></td>';
            if($row[speed]==$max_speed){
                echo    '<td class="essential bg-yellow">'.$row[speed].'</td>';
            }else{
                echo    '<td class="essential">'.$row[speed].'</td>';
            }
            if($row[keystroke]==$max_keystroke){
                echo    '<td class="essential bg-green">'.$row[keystroke].'</td>';
            }else{
                echo    '<td class="essential">'.$row[keystroke].'</td>';
            }
            if($row[length]==$min_length){
                echo    '<td class="essential bg-red">'.$row[length].'</td>';
            }else{
                echo    '<td class="essential">'.$row[length].'</td>';
            }
            if($row[backspace]==$min_backspace){
                echo    '<td class="essential bg-blue">'.$row[backspace].'</td>';
            }else{
                echo    '<td class="essential">'.$row[backspace].'</td>';
            }
            echo    '<td>'.$row[wrong].'</td>';
            echo    '<td class="center">'.$row[wordcount].'</td>';
            echo    '<td class="center">'.$row[timecost].'</td>';
            echo    '<td class="center">'.date("m-d H:i",strtotime($row[timestart])).'</td>';
            if($row[repeat]==$max_repeat){
                echo    '<td class="center bg-green">'.$row[repeat].'</td>';
            }else{
                echo    '<td class="center">'.$row[repeat].'</td>';
            }
            echo    '<td>'.$row[score].'</td>';
            echo    '<td>'.$row[scoresum].'</td>';
            echo '</tr>';
            $index++;
        }

    最终的显示效果

    screenshot

    2019-07-17 19:43:11
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
PHP 2017.北京 全球开发者大会——高可用的PHP 立即下载
PHP安全开发:从白帽角度做安全 立即下载
复杂PHP系统性能瓶颈排查及优化 立即下载