Skip to content

[YAML] Array keys disappeared when dump #18843

Closed
@flat1701

Description

@flat1701

I executed like below.

$array = ['foo' => ['bar' => [1 => 2, 'baz' => 3]]];
echo Yaml::dump($array);

I expected

foo:
bar: { 1: 2, baz: 3 }

but result is

foo:
bar: [2, 3]

I edited Yaml/Inline.php like below, I got wanted result.

@@ -159,9 +159,7 @@
     {
         // array
         $keys = array_keys($value);
-        if ((1 == count($keys) && '0' == $keys[0])
-            || (count($keys) > 1 && array_reduce($keys, function ($v, $w) { return (int) $v + $w; }, 0) == count($keys) * (count($keys) - 1) / 2)
-        ) {
+        if ($keys === range(0, count($keys)-1)) {
             $output = array();
             foreach ($value as $val) {
                 $output[] = self::dump($val, $exceptionOnInvalidType, $objectSupport);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions