开发者

For Loop Custom Fields

i need help for this task.

i need a for loop who picks the closest duration

sample we have a duration with 120 seconds the 120 seconds are on every calculation divided by 8 so here is an example

120 Duration

Here are the 8 closest values 15 30 45 60 75 90 105 120

How i can realize this i have all tested

<?php
$count = 1;
$duration = 120;
$temp 开发者_StackOverflow中文版= 0;
for ($x = 1; $x < 120; $x++) {
    #$j = $x / 8;
    $temp = $x / 8;
    echo '<pre>' . ($temp) . '</pre>';

    if ($count == 8) {
        break;
    }
    $count++;
}
?>


Your entire loop is totally redundant. Why not just this:

<?php
for ($i = 0; i < 8; ++$i)
{
   echo '<pre>' . (15 * ($i+1)) . '</pre>';
}
?>

You can use $i directly as a counter in the loop.


Your question is quite unclear and the provided code doesn't go from 1->120 since you break it after 8 iterations. To get the values 15 30 45 60 75 90 105 120 from the base 120 you would need something like this:

$result = array();
$duration = 120; //the duration in seconds as provided in the example
$divider = 8; //the divider 8 as provided by the example

for ($i = 1; $i <= $divider; $i++) {
    //This will give 1* 120/8 = 15 for the first run
    //2* 120/8 = 30 for the second and so on
    $result[] = (int) $i * $duration / $divider;
}


would the following work?

function sample($max, $count) {
  $samples = array();
  for($i = 1; $i <= $count; ++i) {
    $samples[] = (int)($max / $count * $i);
  }
  return $samples;
}


Do you mean something like

$result = array();
for ($i = 1; $i <= 8; $i++) {
  $result[] = (int) ($duration / 8) * $i;
}

?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜