-3

我有以下代码:

function GetPercent($Arg){
        $Count = count($Arg);
        return /* Confused here */;
    }
        $Test_Array = array(
          "ID" => 1,
          "User" => "Test",
          "Perm" => 1,
          "Test" => "String"

        );

我最终将使用 填充 HTML 表格列count($Test_Array),但我需要将百分比放入表格属性中:

<td align=left style="width:XX%">

但是,我将如何计算百分比?

4

1 回答 1

1

使用 floor() 向下舍入,这样总百分比不会超过 100%,只需将数组传递给函数即可获得平均宽度。

<?php

function GetPercent($Arg){
    $Count = count($Arg);
    return floor( 100 / $Count );
}
$Test_Array = array(
  "ID" => 1,
  "User" => "Test",
  "Perm" => 1,
  "Test" => "String"
);

$average_widths = GetPercent($Test_Array); // in this case will return 25

// ...table tags here etc etc etc

// output results
foreach( $Test_Array as $key => $value ) {
    echo '<td align=left style="width:' . $average_widths . '%">';
    echo $key . ' -> ' . $value;
    echo '</td>';
}
?>
于 2013-10-07T01:09:09.350 回答