我在实现 for 循环并将结果值存储在数组中时遇到问题。请看看我下面的代码:
$pv_numbers = array();
$a=100;//these numbers are just examples, the variables are obtained from some code
$b=5;
$c=10;
for( $d = 0; $d<=($a/$b); $d++ ) { // $a/$b should define the maximum $d number allowed
$f=(($a-($d*$c))/$b) ; //$f should be cover all those values from $d=0 to max
$pv_numbers[] = $f; //then the values stored in an array for use on another function
}
?>
代码似乎将 $d 的最后一个值存储在数组中。
如果你 var_dump($pv_numbers) 你得到 20 个值,从 20 开始到 -20 结束,每次有效地减去 2。
array(21) { [0]=> int(20) [1]=> int(18) [2]=> int(16) … [19]=> int(-18) [20]=> int( -20) }
让我们来看看前几次迭代
$f = ((100-(0*10))/5) // $f 是 20
$f = ((100-(1*10))/5) // $f 是 18
继续循环 20 次迭代或直到 $d <= 20 ($a/$b)
您可能指的是与 $d 的值相等的键,不是因为 $d 而是因为每次调用 $pv_numbers[] = $f 时它都会添加一个键,并且 $d 正在添加一个(感谢 $ d++) 每次迭代。
这不是你期望发生的吗?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句