php - 将迭代次数转换为有限范围(如星期几)

标签 php range limit

我需要将迭代次数转换为 1 到 7 之间的范围。

$y = keepInRange(1, 7, $i)

结果输入->输出预计如下

  • 1 -> 1
  • ...
  • 7 -> 7
  • 8 -> 1
  • 9 -> 2
  • ...
  • 14 -> 7
  • 15 -> 1

我已经尝试过以下但没有成功:

min(7, max(1, $numberToStr[$i])) (all output 1)
$y = $i % 7 (all outputs 0, Edit: this was a mistake by me, its the solution when +1 is added.)

最佳答案

试试这个

<?php
$num = 15;
$res= $num%7;
if($res == 0)
{
    echo "7";
}
else
{
    echo $res;
}

https://3v4l.org/dDGs3

希望对你有帮助

关于php - 将迭代次数转换为有限范围(如星期几),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38183563/

相关文章:

php - 通过 PHP 检查 Gmail IMAP 循环中的新邮件

php - 如何在一个查询中组合 2 个选择?

caching - 无法播放 Service Worker 中的缓存音频

delphi - 在 Delphi 中如何将范围作为参数传递?

android - android:播放的声音总数有限制吗?

excel - 克服 VBA 输入框字符限制

python - 显示随机选择(Python)

php - 调用未定义的方法 Template::append_css()

php - Codeigniter 项目文件夹

Python 3.0 : Looping list index that is "out of range"