我正在尝试创建一个具有以下结构的数组:
[10] => Array
(
[10] => test key
...
[100] => test key
)
[20] => Array
(
[10] => test key
...
[100] => test key
)
[30] => Array
(
[10] => test key
...
[100] => test key
)
...
这是我用来创建这样的结构的循环:
$array = array();
for ($x = 0.1; $x <= 1; $x+=0.1) {
$index = $x*100;
for ($z = 10; $z <= 100; $z+=10) {
$array[(int)$index][$z] = 'test key';
}
}
不幸的是,一旦它应该达到90和100索引,我收到的输出就像这样:
[80] => Array
(
[10] => test key
...
[100] => test key
)
[89] => Array
(
[10] => test key
...
[100] => test key
)
[99] => Array
(
[10] => test key
...
[100] => test key
)
为什么计算指数为89和99?不应该分别是90和100吗?
*编辑 我使用花车作为我们用来进行一些计算的百分比的测量。我知道我也可以增加10个增量,但这需要我最终再次除以100以获得浮动
答案 0 :(得分:2)
不确定为什么要使用$x
的浮动广告...只需从10
开始,然后逐步10
,直至找到100
。
$array = array();
for ($x = 10; $x <= 100; $x+=10) {
$f = ((float)$x) / 100.0; # calculate float value here
for ($z = 10; $z <= 100; $z+=10) {
$array[$x][$z] = 'test key';
}
}
正如其他人所指出的那样,你所看到的很可能是由于浮点运算舍入。
答案 1 :(得分:0)
为什么计算指数为89和99?不应该分别是90和100?
我认为这是因为您使用小数作为x值,为什么不使用整数值?
$array = array();
for ($x = 10; $x <= 100; $x+=10) {
for ($z = 10; $z <= 100; $z+=10) {
$array[$x][$z] = 'test key';
}
}
答案 2 :(得分:0)
稍微更现代的语法:
<?php
$array = [];
for ($x = 10; $x <= 100; $x+=10) {
for ($z = 10; $z <= 100; $z+=10) {
$array[$x][$z] = 'test key';
}
}
print_r($array);
?>
输出:
Array
(
[10] => Array
(
[10] => test key
[20] => test key
[30] => test key
[40] => test key
[50] => test key
[60] => test key
[70] => test key
[80] => test key
[90] => test key
[100] => test key
)
[20] => Array
(
[10] => test key
[20] => test key
[30] => test key
[40] => test key
[50] => test key
[60] => test key
[70] => test key
[80] => test key
[90] => test key
[100] => test key
)
[30] => Array
(
[10] => test key
[20] => test key
[30] => test key
[40] => test key
[50] => test key
[60] => test key
[70] => test key
[80] => test key
[90] => test key
[100] => test key
)
[40] => Array
(
[10] => test key
[20] => test key
[30] => test key
[40] => test key
[50] => test key
[60] => test key
[70] => test key
[80] => test key
[90] => test key
[100] => test key
)
[50] => Array
(
[10] => test key
[20] => test key
[30] => test key
[40] => test key
[50] => test key
[60] => test key
[70] => test key
[80] => test key
[90] => test key
[100] => test key
)
[60] => Array
(
[10] => test key
[20] => test key
[30] => test key
[40] => test key
[50] => test key
[60] => test key
[70] => test key
[80] => test key
[90] => test key
[100] => test key
)
[70] => Array
(
[10] => test key
[20] => test key
[30] => test key
[40] => test key
[50] => test key
[60] => test key
[70] => test key
[80] => test key
[90] => test key
[100] => test key
)
[80] => Array
(
[10] => test key
[20] => test key
[30] => test key
[40] => test key
[50] => test key
[60] => test key
[70] => test key
[80] => test key
[90] => test key
[100] => test key
)
[90] => Array
(
[10] => test key
[20] => test key
[30] => test key
[40] => test key
[50] => test key
[60] => test key
[70] => test key
[80] => test key
[90] => test key
[100] => test key
)
[100] => Array
(
[10] => test key
[20] => test key
[30] => test key
[40] => test key
[50] => test key
[60] => test key
[70] => test key
[80] => test key
[90] => test key
[100] => test key
)
)