PHP循环#到100,然后循环另一个#

PHP循环#到100,然后循环另一个#,php,Php,我正在开发一个快速函数,该函数将接受一个数字$max,如果低于100,则按5最多$max计数 如果$max大于100,则按5计数,直到达到100,然后按25计数,直到达到$max 例如: public function getDenominations($max) { $start = 20; $output = []; // If max is less than 100, count by 5's if($max <= 100 ){ f

我正在开发一个快速函数,该函数将接受一个数字
$max
,如果低于
100
,则按
5
最多
$max
计数

如果
$max
大于
100
,则按5计数,直到达到
100
,然后按25计数,直到达到
$max

例如:

public function getDenominations($max)
{
    $start = 20;
    $output = [];

    // If max is less than 100, count by 5's
    if($max <= 100 ){
        for($i = $start; $i <= $max; $i += 5) {
            array_push($output, $i);
        }
        return $output;
    // Max is greater than 100, count by 5's up to 100, then by 25's
    }else{
        for($i = $start; $i <= $max; $i += 25) {
            array_push($output, $i);
        }
        return $output;
    }

}
公共函数获取面额($max)
{
$start=20;
$output=[];
//如果最大值小于100,则按5计数

如果($max我已经简化了你的代码。你不需要两个循环,你可以继续循环,并改变你用(5或25)增加当前值的多少,直到达到最大值

例如,类似这样的事情:

$max = 130;
$output = [];
$current = 0;

// Keep looping until we hit max, or break out of the loop
while ($current <= $max) {
    // Find out if we should increase the current value with 5 or 25
    if ($current < 100) {
        $current += 5;
    }
    else {
        $current += 25;
    }

    // Make sure we do not add anything above the current max (e.g. max = 120, current = 110 + 25 = 135)
    if ($current > $max) {
        break;
    }

    // Add the current value to the output array
    $output[] = $current;
}

print_r($output);

像这样-在javascript中

max = 200
for (i = 1; i < max; i++) {
    out = i * 5;
    console.log(out <= 100 ? out : (i * 5 * 5) - 400);
}

5
10
15
20
25
30
35
40
45
50
55
60
65
70
75
80
85
90
95
100
125
150
175
200
225
250
275
300
325
350
375
400
425
450
475
500
525
550
575
600
625
650
675
700
max=200
对于(i=1;iconsole.log(out您需要的功能可以简单地编写为:

function getDenominations($max)
{
    $start = 20;
    $output = [];

    // Count by 5 from $start to 100 or $max, whichever is smaller
    for ($i = $start; $i <= min($max, 100); $i += 5) {
        $output[] = $i;
    }

    // Continue counting by 25 from 125 to $max, if $max is large enough
    for ($i = 125; $i <= $max; $i += 25) {
        $output[] = $i;
    }

    return $output;
}
函数获取面额($max)
{
$start=20;
$output=[];
//从$start到100或$max,以较小者为准,按5计算

对于($i=$start;$i),当前输出是什么?或者,换句话说,问题出在哪里?我看不到任何问题。编辑:请添加当前输出。这个问题用php标记?
function getDenominations($max)
{
    $start = 20;
    $output = [];
    $incr = 5;
    $flag = false;

    for ($i = $start; $i <= $max; $i += $incr) {
        array_push($output, $i);

        if( $i >= 100 && $flag === false) {
            $incr = 25;
            $flag = true;
        }

    }
    return $output;
}

print_r(getDenominations(200));
Array
(
[0] => 20
[1] => 25
[2] => 30
[3] => 35
[4] => 40
[5] => 45
[6] => 50
[7] => 55
[8] => 60
[9] => 65
[10] => 70
[11] => 75
[12] => 80
[13] => 85
[14] => 90
[15] => 95
[16] => 100
[17] => 125
[18] => 150
[19] => 175
[20] => 200
)
function getDenominations($max)
{
    $start = 20;
    $output = [];

    // Count by 5 from $start to 100 or $max, whichever is smaller
    for ($i = $start; $i <= min($max, 100); $i += 5) {
        $output[] = $i;
    }

    // Continue counting by 25 from 125 to $max, if $max is large enough
    for ($i = 125; $i <= $max; $i += 25) {
        $output[] = $i;
    }

    return $output;
}