如何创建一个php函数来计算,然后为任何给定范围(比如4英尺到7英尺)生成一个表示英制和其等效公制测量(对于人类)的数组?
例如:
Array
(
[1] => 4'8"(142cm)
[2] => 4'9"(144.5cm)
[3] => 4'10"(147cm)
)
......等等。重量(磅/公斤)的相同例子也是如此。如果有人能给我一个良好的开端,我会很感激。
答案 0 :(得分:1)
这可能会指出你正确的方向..没有经过测试,但它应该足以让你开始。非常简单的概念。我开始用脚+英寸琴弦开始 - 现在你应该能够弄清楚如何在那里买到米。
// $startHeight and $endHeight are in inches
function createRange($startHeight,$endHeight){
// calculate the difference in inches between the heights
$difference = $endHeight - $startHeight;
// create an array to put the results in
$resultsArray;
//create a loop with iterations = $difference
for($i=0;$i<$difference;$i++)
{
// create the current height based on the iteration
$currentHeight = $startHeight + $i;
// convert the $currentHeight to feet+inches
// first find the remainder, which will be the inches
$remainder = ($currentHeight % 12);
$numberOfFeet = ($currentHeight - $remainder)/12;
// build the feet string
$feetString = $numberOfFeet.'''.$remainder.'"';
// now build the meter string using a similar method as above
// and append it to $feetString, using a conversion factor
// add the string to the array
$resultsArray[] = $feetString;
}
// return the array
return $resultsArray;
}