我试图在同一个for循环中增加两个独立的数字。例如,我希望第一个数字是:
0,2,4,6,8,10 etc
我希望第二个数字是:
1,3,5,7,9 etc
我已经尝试了这里的建议,但可以实现:
How to increment a number by 2 in a PHP For Loop
迄今为止我的代码:
$count = count($toyList); echo $count; for($i=0; $i<$count; $i++){ $json = '{"toy": {"toyname":"' . $toyList[$i+2] . ',"status":"' . $toyList[$i+3] . '"}}'; }
任何帮助都将不胜感激。谢谢
引入一个额外的变量,并在循环中增加该变量,在使用完它之后,无论您打算做什么。我手头没有PHP环境,但它应该是这样的:
$count = count($toyList); echo $count; $j = 1; for($i=0; $i<$count; $i++){ $json = '{"toy": {"toyname":"' . $toyList[$i+2] . ',"status":"' . $toyList[$i+3] . '"}}'; $j = $j + 2; }