2017-02-20 41 views
0

我試圖創建一個具有以下結構的陣列:For循環分配錯誤的數組索引

[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才能獲得浮點數

+1

[閱讀,學習和了解(https://en.wikipedia.org/wiki/Floating_point#Accuracy_problems );那麼明白當從float到int的投射時,PHP會下降 –

+0

但是爲什麼你仍然使用浮動? –

+0

爲什麼在10年代這樣一個複雜的遞增方式 – nogad

回答

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'; 
    } 
} 

正如其他人指出的,你所看到的很可能是由於浮點算術舍入。

+0

我使用浮點數,因爲這是我們用來進行一些計算的百分比度量。我知道我也可以增加10個增量,但這需要我最終再次除以100才能獲得浮點數 –

+1

明白了......在這種情況下,需要時從「int」轉換爲「float」。查看更新的答案。 – dana

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'; 
    } 
} 
+0

我使用的是浮點數,因爲這是我們用來進行一些計算的百分比度量。我知道我也可以增加10個增量,但這需要我最終再次除以100以獲得浮點數 –

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 
     ) 

)