好的。基本上,我想在PHP中将数字1-3赋给数字的无限级数。我该怎么做呢?
我想分配如下。
核心- 1.补充- 1.
核心- 2.补充- 2.
核心- 3.补充- 3.
核心- 4.补充- 1。
核心- 5.补充- 2.
核心- 6.补充- 3.
核心- 7.补充- 1.
等
干杯
发布于 2011-03-15 00:39:43
您可以使用modulo operation:
$num = ($index % 3) + 1将始终返回一个介于1和3之间的数字。
发布于 2011-03-15 00:42:52
听起来像是一个基本的模函数。
模数被定义为除法的余数,并在PHP中使用百分号指定。
示例代码:
<?php
for($loopcount = 1; $loopcount<=$max; $loopcount++) {
print "Counter: ".$loopcount." ... Counter mod 3: ".($loopcount % 3)."<br />\n";
}
?>会给你一个0,1,2,0,1,2等的序列。只需将1加到mod结果中,得到1,2,3,1,2,3等。
因此,要完全按照您的要求生成:
<?php
for($loopcount = 1; $loopcount<=$max; $loopcount++) {
print "Core - ".$loopcount.". Supplement - ".(($loopcount % 3)+1).".<br />\n";
}
?>请参阅PHP手册:http://www.php.net/manual/en/language.operators.arithmetic.php
发布于 2011-03-15 00:40:03
您可以使用while循环:
$arrayNum = array(); //aray of numbers
$max_number = 100; //max of numbers (you can set this to any value)
$i = 0;
while($i < $max_number) {
$arrayNum[$i] = ($i % 3) + 1; // the +1 ensures that none = 0
echo "Core - $i. Supplement - {$arrayNum[$i]}"; //echo result
$i ++;
}https://stackoverflow.com/questions/5301542
复制相似问题