I'm trying to use the Symfony Yaml dump function to output some nested php array data.
use \Symfony\Component\Yaml\Yaml;
echo Yaml::dump([
'arr'=>[],
'foo'=>'bar',
]);
However, the dumped YAML contains an empty object:
arr: { }
foo: bar
while I want an empty list:
arr: []
foo: bar
I tried using the Yaml::DUMP_OBJECT_AS_MAP flag, using the ArrayObject instead of array literals, and using an EmptyIterator, all to no avail.
I found two closed bugs related to this: https://github.com/symfony/symfony/issues/9870 and https://github.com/symfony/symfony/issues/15781, but the solutions there don't seem to work, or are a bit too hacky and brittle for my taste (str_replace on the YAML output, brrrr)
I have a simple testcase with what I tried so far: https://github.com/ComaVN/php-yaml-empty-array
Any suggestions on how to fix this?
As of Symfony 3.3 (released May 2017) you can use the new Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE option:
Yaml::dump($object, 2, 4, Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
This feature was added in PR 21471.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With