0

I am trying to create an array with the following structure:

[10] => Array
    (
        [10] => test key
        ...
        [100] => test key
    )

[20] => Array
    (
        [10] => test key
        ...
        [100] => test key
    )

[30] => Array
    (
        [10] => test key
        ...
        [100] => test key
    )
...

This is the loop that I use in order to create such structure:

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

Unfortunately, the output that I receive looks like this once it is supposed to reach the 90 and 100 index:

[80] => Array
    (
        [10] => test key
         ...
        [100] => test key
    )

[89] => Array
    (
        [10] => test key
         ...
        [100] => test key
    )

[99] => Array
    (
        [10] => test key
         ...
        [100] => test key
    )

Why does it calculate the index to be 89 and 99? Shouldn't it be 90 and 100 respectively?

*edit I am using floats as that's the measurement of percentages that we use to make some calculations. I know I could go by 10 increments as well, but that would require me to eventually divide by 100 again in order to get the float

5
  • 1
    Read, learn and understand; then appreciate that PHP rounds down when casting from float to int Commented Feb 20, 2017 at 23:52
  • But why are you using floats anyway? Commented Feb 20, 2017 at 23:54
  • why such a convoluted way of incrementing in 10's Commented Feb 20, 2017 at 23:56
  • I am using floats as that's the measurement of percentages that we use to make some calculations. Commented Feb 20, 2017 at 23:59
  • 1
    Use integers, then convert to floats only when you need to Commented Feb 21, 2017 at 0:06

3 Answers 3

2

Not sure why you are using floats with $x... Just start at 10 and step by 10 until you hit 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';
    }
}

As others have pointed out, what you are seeing is most likely due to floating-point arithmetic rounding.

Sign up to request clarification or add additional context in comments.

2 Comments

I am using floats as that's the measurement of percentages that we use to make some calculations. I know I could go by 10 increments as well, but that would require me to eventually divide by 100 again in order to get the float
Got it... In that case, convert from int to float when needed. See updated answer.
0

Why does it calculate the index to be 89 and 99? Shouldn't it be 90 and 100 respectively?

I think that it occurs because you use a decimal for the x value, why you don't use an integer value?

$array = array();
for ($x = 10; $x <= 100; $x+=10) {
    for ($z = 10; $z <= 100; $z+=10) {
        $array[$x][$z] = 'test key';
    }
}

1 Comment

I am using floats as that's the measurement of percentages that we use to make some calculations. I know I could go by 10 increments as well, but that would require me to eventually divide by 100 again in order to get the float
0

Slightly more modern syntax:

<?php

$array = [];
for ($x = 10; $x <= 100; $x+=10) {
    for ($z = 10; $z <= 100; $z+=10) {
        $array[$x][$z] = 'test key';
    }
}

print_r($array);
?>

Outputs:

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
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.