【问题标题】:Creating dynamic associative array in configuration在配置中创建动态关联数组
【发布时间】:2019-09-30 11:38:45
【问题描述】:

我得到了这样的 yml 配置:

test_array:
    dynamic_key:
        - 'this_is_value'
        - 'it_is_also_a_value'
    second_dynamic_key:
        - 'yop, value'
        - 'another value'

配置后,我想得到一个这样的数组:

$iNeedToGetArrayLikeThis = [
    'dynamic_key' => [
        'this_is_value',
        'it_is_also_a_value'
    ],
    'second_dynamic_key' => [
        'yop, value',
        'another value'
    ]
];

但我明白了:

$whatIGet = [
    [
        'dynamic_key' => [
            'this_is_value',
            'it_is_also_a_value'
        ],
        'second_dynamic_key' => [
            'yop, value',
            'another value'
        ]
    ],
];

我尝试使用->useAttributeAsKey(),但也没有用。

我的代码配置代码:

->children()
   ->arrayNode('test_array')
       ->ignoreExtraKeys(true)
          ->arrayPrototype()
             ->prototype('scalar')
             ->end()
           ->end()
       ->end()
   ->end()
->end()

【问题讨论】:

  • 你能用$whatIGet[0]吗?

标签: php symfony dependency-injection yaml


【解决方案1】:

你可以写得很简单:

test_array:
    - dynamic_key: ['this_is_value', 'it_is_also_a_value']
    - second_dynamic_key: ['yop, value', 'another value']

demo

test_array:
    - dynamic_key: 
        - 'this_is_value'
        - 'it_is_also_a_value'
    - second_dynamic_key: 
        - 'yop, value'
        - 'another value'

demo

【讨论】:

    猜你喜欢
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 2011-07-28
    相关资源
    最近更新 更多