我有以下代码:
function GetPercent($Arg){
$Count = count($Arg);
return /* Confused here */;
}
$Test_Array = array(
"ID" => 1,
"User" => "Test",
"Perm" => 1,
"Test" => "String"
);
我最终将使用count($Test_Array)
填充HTML表格列,但我需要将百分比放在表格中:
<td align=left style="width:XX%">
BUt,我将如何计算百分比?
答案 0 :(得分: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>';
}
?>