循环中的最大值和最小值

时间:2014-02-05 09:34:15

标签: php

我计算每列的平均值,最小值,最大值。 最大值和最小值的第16列值出错。平均值正常工作。 错误仅适用于具有大型数组的最后一列,我也会收到通知错误。

下面的代码

mysql_connect("localhost", "root", "") or
die("Could not connect: " . mysql_error());
mysql_select_db("db");
$qry  =  mysql_query("select blob_field from table where id=1");
$arr = mysql_fetch_row($qry) ;

$rt  =  explode("\n",$arr[0]);
$header_arr1 = explode(',',$rt[0]);
$header1 = array();
$channels = count($header_arr1)-2 ;
$header1[]  = join(',', array_slice($header_arr1, 2));
//Only Header
$arr_values = array_values($rt);

$array_slice = array_slice($arr_values, 1);
echo "<pre>";
//print_r($array_slice);

$result_array  = array();
for($i=0;$i<count($array_slice);$i++)
{
    //echo $i;
    $result_array[] =  join(',',array_slice(explode(',', $array_slice[$i]), 2));

}

//print_r($result_array);
//$array_combined  = array_merge($header1,$result_array);
$token = array( 'Average', 'Max', 'Min');

foreach($result_array as $key=>$val)
{
    $res_arr[] = explode(",",$val);
}

$avArray = array();
$res_arr= array(
    0=>array(1,2,3,6,7,8,9,11,33,44,55,66,77,88,16,18),
    1=>array(17,52,38,666,79,8,9,15,33,44,55,66,76,88,16,18),
    2=>array(17,562,538,7666,579,688,9,15,933,44,55,660,76,808,106,108),
);
foreach ($res_arr as $k=>$subArray)
{
    foreach ($subArray as $id=>$value)
    {
        //print_r($id);

        $avArray[$id] += $value/count($res_arr); //Notice: Undefined offset: 3 

    }

}
$token = array( 'Average', 'Max', 'Min');
$num = $avArray;
$tc=16;
?>
<table border="1" style="border-collapse: collapse;">
    <thead>
        <tr>
            <th>Channels</th>
            <?php for($j=0;$j<=$tc-1;$j++):?>
            <th>CH<?= $j+1;?></th>
            <?php endfor;?>
        </tr>
    </thead>
    <tbody>
    <?php

    echo "<tr><td>Average</td>";
    for($i=0; $i<=$tc-1;$i++)
    {
        echo "<td>" .round($num[$i],2). "</td>";

    }

    echo "<tr><td>Max</td>";
    $arr_max = array();
    foreach($res_arr as $subArray)
    {
        foreach($subArray as $k=>$v) {
            $arr_max[$k][] = $v;
        }
    }
    $max_array  = array_map('max', $arr_max);


    for($i=0; $i<=$tc-1;$i++)
    {

        echo "<td>" .$max_array[$i]. "</td>";

    }

    echo "<tr><td>Min</td>";
    $arr_min = array();
    foreach($res_arr as $subArray)
    {
        foreach($subArray as $k=>$v1) {
            $arr_min[$k][] = $v1;
        }
    }
    $min_array  = array_map('min', $arr_min);

    for($g=0; $g<= $tc-1;$g++)
    {
        echo "<td>" .$min_array[$g]. "</td>";

    }

    ?>
    </tbody>
</table>

1 个答案:

答案 0 :(得分:0)

请参阅内联注释以获取解释。

$avArray = array();
$res_arr= array(
    0=>array(1,2,3,6,7,8,9,11,33,44,55,66,77,88,16,18),
    1=>array(17,52,38,666,79,8,9,15,33,44,55,66,76,88,16,18),
    2=>array(17,562,538,7666,579,688,9,15,933,44,55,660,76,808,106,108),
);
/*
foreach ($res_arr as $k=>$subArray){
    foreach ($subArray as $id=>$value){
        // this causes "Notice: Undefined offset: 0...15" because you can't add to an undeclared value
        $avArray[$id] += $value/count($res_arr);
    }
}
*/
$res_count=count($res_arr);  // declare this outside of the loop for efficiency
foreach ($res_arr as $k=>$subArray){
    foreach ($subArray as $id=>$value){
        if($k==0){
            $avArray[$id]=$value;  // declare first round of values
        }else{
            $avArray[$id]+=$value;  // add to existing values
            if($k==2){
                $avArray[$id]=$avArray[$id]/$res_count;  // only divide on the last iteration
            }
        }
    }
}
print_r($avArray);