为什么此数组键设置为零?

时间:2014-01-03 21:32:27

标签: php arrays

function getMonths() {
        $data[] = array('key' => 01, 'name' => 'January');
        $data[] = array('key' => 02, 'name' => 'February');
        $data[] = array('key' => 03, 'name' => 'March');
        $data[] = array('key' => 04, 'name' => 'April');
        $data[] = array('key' => 05, 'name' => 'May');
        $data[] = array('key' => 06, 'name' => 'June');
        $data[] = array('key' => 07, 'name' => 'July');
        $data[] = array('key' => 08, 'name' => 'August');
        $data[] = array('key' => 09, 'name' => 'September');
        $data[] = array('key' => 10, 'name' => 'October');
        $data[] = array('key' => 11, 'name' => 'November');
        $data[] = array('key' => 12, 'name' => 'December');
        error_log("data:");
        error_log(print_r($data,1));

        return $data;
    }

由于某些原因我无法理解,当我print_r $data数组而不是08和09时,八月和九月的键是0。

[03-Jan-2014 15:28:26] data:
[03-Jan-2014 15:28:26] Array
(
    [0] => Array
        (
            [key] => 1
            [name] => January
        )

    [1] => Array
        (
            [key] => 2
            [name] => February
        )

    [2] => Array
        (
            [key] => 3
            [name] => March
        )

    [3] => Array
        (
            [key] => 4
            [name] => April
        )

    [4] => Array
        (
            [key] => 5
            [name] => May
        )

    [5] => Array
        (
            [key] => 6
            [name] => June
        )

    [6] => Array
        (
            [key] => 7
            [name] => July
        )

    [7] => Array
        (
            [key] => 0
            [name] => August
        )

    [8] => Array
        (
            [key] => 0
            [name] => September
        )

    [9] => Array
        (
            [key] => 10
            [name] => October
        )

    [10] => Array
        (
            [key] => 11
            [name] => November
        )

    [11] => Array
        (
            [key] => 12
            [name] => December
        )

)

我在这里缺少什么?

4 个答案:

答案 0 :(得分:2)

你需要把它们放在引号中。 '08''09'等。否则PHP认为您使用的是八进制数字。

八进制:http://en.wikipedia.org/wiki/Octal

PHP Ints:http://us2.php.net/manual/en/language.types.integer.php

答案 1 :(得分:2)

0开头的文字是八进制文字。十进制是一个10位数的数字系统,八进制是一个只有8位数的数字系统。因此,0809等是不存在的数字。一个类似的例子是不存在的十六进制文字0xG

只需像普通人一样写89;)

顺便说一句,这似乎是使用数组的一种相当奇怪的方式。为什么不:

function getMonths()
{
    return Array(
        1  => 'January',
        2  => 'February',
        // ...
        12 => 'December'
    );
}

如果您正在与需要特定表示这些键的某个外部系统连接,其中该特定表示是一个前导零的字符串,那么使用字符串

function getMonths()
{
    return Array(
        '01' => 'January',
        '02' => 'February',
        // ...
        '12' => 'December'
    );
}

答案 2 :(得分:1)

以零(0)开头的数字在PHP中是八进制的,因此0809是无效数字,请查看更多内容:http://www.php.net/manual/en/language.types.integer.php

修复:删除前导零或用单引号括起来

答案 3 :(得分:0)

这是您的功能的替代

function getMonths() {
    for ($x=0; $x < 12; $x++) {

        $time = strtotime('+' . $x . ' months', strtotime(date('Y-M' . '-01')));
        $key = date('m', $time);
        $name = date('F', $time);
        $months[$key] = $name;
    }
    return $months ;
}

输出为

Array ( [01] => January [02] => February [03] => March [04] => April [05] => May [06] => June [07] => July [08] => August [09] => September [10] => October [11] => November [12] => December )