Roel Harbers
Roel Harbers

Reputation: 1054

How to dump an empty array using Symfony Yaml

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?

Upvotes: 4

Views: 1632

Answers (2)

Christian Schmidt
Christian Schmidt

Reputation: 520

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.

Upvotes: 7

btedu
btedu

Reputation: 17

Try putting that in some form quotes, so that it can be extracted at a later time?

'arr'=>'[]',

Upvotes: -2

Related Questions