【问题标题】:Generate Weight & Height Array PHP生成体重和身高数组 PHP
【发布时间】:2012-09-07 13:27:16
【问题描述】:

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

例如:

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

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

【问题讨论】:

  • 您对社区的实际期望是什么?转换非常简单(数学很简单),数组生成也很简单(foreachfor
  • @zerkms 尽管您的评论似乎无益且有些粗鲁,但您可能是对的,因为这对您来说是一件微不足道的事情。然而,对我来说,情况并不相同。我无法理解实现这一目标的最佳方法是什么。这是一个问题,在一个回答他们的社区中。如果它是如此微不足道,请教我一些东西,并为自己赢得一些分数和一种良好的感觉,即你今天帮助了某人,而不是批评我对这个社区的看法和判断我的能力。
  • @Lea:好吧,将您的问题拆分为较小的问题。让我们说第一个:生成一行数字。另一个:假设数字是以英尺为单位的长度 - 将其转换为米。你被其中哪一个困住了?编程中的每个问题都由较小的问题组成。而每个程序员只是迭代地一个一个地解决小问题。 PS:我问你对我们有什么期望,因为你现在的问题看起来像:“这是一项任务,请为我做这件事”
  • @zerkms 我在问题的底部要求先行一步,而不是“用勺子喂”。这个问题很明确,问的是“如何?”,而不是“有人可以这样做......吗?”。我正在寻找有关如何实现这一目标的想法,以前没有做过,我不知道从哪里开始,找不到关于它的讨论。可能在我的脑海中让这一切变得更加复杂,只需要在正确的方向上稍微推动一下。我会听从你的建议,试着分解一切,然后从那里开始。

标签: php


【解决方案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;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    • 2012-06-24
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多