简体   繁体   English

parameters.yml 中的数字索引损坏了 Symfony2

[英]Number index in parameters.yml corrupted Symfony2

I have in my parameters.yml an array of months which make the relation between the php function date('m') (months number with leading zero) and a letter choosen by default for generating a code.我在我的 parameters.yml 中有一个月份数组,它使 php 函数 date('m') (带前导零的月份数)和默认选择的字母之间的关系生成代码。

months:
    01: 'A'
    02: 'B'
    03: 'C'
    04: 'D'
    05: 'E'
    06: 'F'
    07: 'G'
    08: 'H'
    09: 'I'
    10: 'J'
    11: 'K'
    12: 'L'

And here is the result of a var_dump($this->getParameters('months')) :这是 var_dump($this->getParameters('months')) 的结果:

array (size=11)数组(大小=11)
1 => string 'A' (length=1) 1 => 字符串“A”(长度=1)
2 => string 'B' (length=1) 2 => 字符串'B'(长度=1)
3 => string 'C' (length=1) 3 => 字符串'C'(长度=1)
4 => string 'D' (length=1) 4 => 字符串 'D'(长度 = 1)
5 => string 'E' (length=1) 5 => 字符串'E'(长度=1)
6 => string 'F' (length=1) 6 => 字符串'F'(长度=1)
7 => string 'G' (length=1) 7 => 字符串'G'(长度=1)
0 => string 'H' (length=1) 0 => 字符串'H'(长度=1)
10 => string 'J' (length=1) 10 => 字符串'J'(长度=1)
11 => string 'K' (length=1) 11 => 字符串“K”(长度=1)
12 => string 'L' (length=1) 12 => 字符串“L”(长度=1)

I finally resolve it removing the leading zero of the index, but is anybody know why the 08 is changed to 0 and 09 index disappear ?我终于解决了它删除了索引的前导零,但是有人知道为什么 08 更改为 0 并且 09 索引消失了吗?

The reason is that if you don't use the quotes 01 , 02 ... 07 are interpreted as base 8 numbers (octal), 08 and 09 are Invalid numeric literal (invalid base 8 number), 10..12 are interpreted as integer numbers, you need use the quotes to fix this: 原因是,如果你不使用引号0102 ... 07被解释为基地8号(八进制), 0809Invalid numeric literal (无效基地8号), 10..12被解释为整数,你需要使用引号来解决这个问题:

months:
    '01': 'A'
    '02': 'B'
    '03': 'C'
    '04': 'D'
    '05': 'E'
    '06': 'F'
    '07': 'G'
    '08': 'H'
    '09': 'I'
    '10': 'J'
    '11': 'K'
    '12': 'L'

See reference here http://symfony.com/doc/current/components/yaml/yaml_format.html#numbers 请参阅此处参考http://symfony.com/doc/current/components/yaml/yaml_format.html#numbers

PHP uses the 0 prefix to denote that the number that follows is in octal. PHP使用0前缀表示后面的数字是八进制。 Thus 8 and 9 are invalid. 因此8和9无效。

Just run echo 08; 只需运行echo 08; or echo 09; 或者echo 09; to see the error output. 查看错误输出。

That's the difference between an integer and a string . 这是整数字符串之间的差异。

Change 01 to '01' and your problem is solved. 更改01'01' ,你的问题就解决了。

But why do you store all alphabetic chars in parameters.yml? 但是为什么要将所有字母字符存储在parameters.yml中? It doesn't seem to be environment related and there is an easy way to get the same array like this: 它似乎与环境无关,并且有一种简单的方法可以获得相同的数组:

$alphas = range('A', 'L');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM