1

如何创建一个计算的 php 函数,然后生成一个数组,该数组代表英制及其等效公制测量值(对于人类)对于任何给定范围(例如 4 英尺到 7 英尺)?

例如:

Array
(
    [1] => 4'8"(142cm)
    [2] => 4'9"(144.5cm)
    [3] => 4'10"(147cm)
)

...等等。重量(磅/公斤)的相同示例也是如此。如果有人可以让我在这方面领先一步,我将不胜感激。

4

1 回答 1

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.'&apos;'.$remainder.'&quot;';

    // 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;

}
于 2012-09-07T05:19:49.550 回答