Cyrille Hejl Cyrille Hejl - 2 months ago 13
YAML Question

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.

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')) :

array (size=11)

1 => string 'A' (length=1)

2 => string 'B' (length=1)

3 => string 'C' (length=1)

4 => string 'D' (length=1)

5 => string 'E' (length=1)

6 => string 'F' (length=1)

7 => string 'G' (length=1)

0 => string 'H' (length=1)

10 => string 'J' (length=1)

11 => string 'K' (length=1)

12 => string 'L' (length=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 ?

Answer

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:

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