如何使用symfony / config解析数组数组?

时间:2015-06-24 02:01:19

标签: php symfony symfony-components

我想解析这个结构的配置:

$config = [
    'streams' => [
        'foo' => [
            [
                'id' => 'some-identifier',
                'type' => 'a',
            ],
            [
                'id' => 'some-other-identifier',
                'type' => 'b',
            ],
        ],
        'bar' => ...,
    ],
];

在此数组中,streams是预定义的键,包含多个任意命名的流的映射。在这种情况下,定义了两个名为foobar的流。

每个流都有一组处理程序。每个处理程序都是一个包含2个属性的地图:idtype

我最终得到了:

$rootNode
    ->children()
        ->arrayNode('streams')
            ->prototype('array')
                ->children()

                ->end()
            ->end()
        ->end()
    ->end()
;

现在我陷入了接下来会发生的事情。

如果我用英语解释它将是:streams是地图数组的地图。

通过我的代码,我可以表达它是一张地图"并坚持如何说出阵列""。

任何提示?

1 个答案:

答案 0 :(得分:1)

那是怎么回事。

$rootNode
    ->children()
        ->arrayNode('streams')
            ->prototype('array')
                ->prototype('array')
                    ->children()
                        ->scalarNode('id')->end()
                        ->scalarNode('type')->end()
                    ->end()
                ->end()
            ->end()
        ->end()
    ->end()
;

请注意,外部prototype('array')没有children()