在配置中创建动态关联数组

问题描述 投票:0回答:1

我有这样的yml配置:

    test_array:
        dinamic_key:
            - 'this_is_value'
            - 'it_is_also_a_value'
        second_dinamic_key:
            - 'yop, value'
            - 'another value'

配置后,我想接收这样的数组:

$iNeedToGetArrayLikeThis = [
    'dinamic_key' => [
        'this_is_value',
        'it_is_also_a_value'
    ],
    'second_dinamic_key' => [
        'yop, value',
        'another value'
    ]
];

但我得到:

$whatIGet = [
    [
        'dinamic_key' => [
            'this_is_value',
            'it_is_also_a_value'
        ],
        'second_dinamic_key' => [
            'yop, value',
            'another value'
        ]
    ],
];

我尝试使用->useAttributeAsKey(),但是didint也起作用。

我的代码配置代码:

->children()
   ->arrayNode('test_array')
       ->ignoreExtraKeys(true)
          ->arrayPrototype()
             ->prototype('scalar')
             ->end()
           ->end()
       ->end()
   ->end()
->end()
php symfony dependency-injection yaml
1个回答
0
投票

您可以写得很简单:

test_array:
    - dinamic_key: ['this_is_value', 'it_is_also_a_value']
    - second_dinamic_key: ['yop, value', 'another value']

demo

test_array:
    - dinamic_key: 
        - 'this_is_value'
        - 'it_is_also_a_value'
    - second_dinamic_key: 
        - 'yop, value'
        - 'another value'

demo

© www.soinside.com 2019 - 2024. All rights reserved.