Skip to content

Conversation

@Deltachaos
Copy link
Contributor

@Deltachaos Deltachaos commented Apr 16, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

Feature: Add Support for preserving array keys as Child <item> Elements in XML Encoder

This PR introduces a new option to the Symfony Serializer's XmlEncoder that allows indexed arrays to be encoded as <item> elements under a single parent element, rather than repeating the parent element for each array item.

Motivation

Currently, encoding an array like:

['person' => [
    ['firstname' => 'Benjamin', 'lastname' => 'Alexandre'],
    ['firstname' => 'Damien', 'lastname' => 'Clay'],
]]

produces the following XML:

<response>
    <person>
        <firstname>Benjamin</firstname>
        <lastname>Alexandre</lastname>
    </person>
    <person>
        <firstname>Damien</firstname>
        <lastname>Clay</lastname>
    </person>
</response>

This structure is not ideal when a clear container-child relationship is required, such as when interoperating with systems that expect such XML structures.

New Behavior

With the new XmlEncoder::PRESERVE_NUMERIC_KEYS option enabled, the same data now encodes to:

<response>
    <person>
        <item key="0">
            <firstname>Benjamin</firstname>
            <lastname>Alexandre</lastname>
        </item>
        <item key="1">
            <firstname>Damien</firstname>
            <lastname>Clay</lastname>
        </item>
    </person>
</response>

Usage

$xml = $encoder->encode($data, 'xml', [
    XmlEncoder::PRESERVE_NUMERIC_KEYS => true,
]);

@Deltachaos Deltachaos requested a review from dunglas as a code owner April 16, 2025 13:49
@carsonbot carsonbot added this to the 7.3 milestone Apr 16, 2025
@Deltachaos Deltachaos force-pushed the feature/numeric-items branch from 72bae95 to 1c234b2 Compare April 16, 2025 13:50
@fabpot fabpot modified the milestones: 7.3, 7.4 May 26, 2025
@Deltachaos Deltachaos force-pushed the feature/numeric-items branch from 1c234b2 to fbea2c7 Compare July 24, 2025 07:46
@OskarStark OskarStark changed the title [Serializer] Add Support for Encoding Arrays as Child <item> Elements in XML Encoder [Serializer] Add support for encoding arrays as child <item> elements in XmlEncoder Jul 24, 2025
@nicolas-grekas nicolas-grekas changed the title [Serializer] Add support for encoding arrays as child <item> elements in XmlEncoder [Serializer] Support preserving array keys with XmlEncoder Sep 15, 2025
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I renamed the option as XmlEncoder::PRESERVE_NUMERIC_KEYS and fixed missing related changes.

@fabpot
Copy link
Member

fabpot commented Sep 17, 2025

Thank you @Deltachaos.

@fabpot fabpot merged commit 84e2730 into symfony:7.4 Sep 17, 2025
10 of 12 checks passed
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull request Sep 25, 2025
…er (dmytro-liashko-dev)

This PR was merged into the 7.4 branch.

Discussion
----------

[Serializer] Support preserving array keys with XmlEncoder

This follows the issue #21388  which is related to the PR symfony/symfony#60228

Commits
-------

96802d1 [Serializer] Support preserving array keys with XmlEncoder
nicolas-grekas added a commit that referenced this pull request Sep 29, 2025
This PR was merged into the 7.4 branch.

Discussion
----------

[Serializer] xml empty array encoding

| Q             | A
| ------------- | ---
| Branch?       | 7.4
| Bug fix?      | yes
| New feature?  | no <!-- if yes, also update src/**/CHANGELOG.md -->
| Deprecations? | no <!-- if yes, also update UPGRADE-*.md and src/**/CHANGELOG.md -->
| Issues        | n/a
| License       | MIT

This fixes a BC break introduced in #60228.

At https://github.com/symfony/symfony/pull/60228/files#diff-b031dbb2186367839adfe20696612866da91513dd315f00ad6df23b5d38fca74R381 the condition change breaks encoding of an empty array:

```diff
-     if (ctype_digit(implode('', array_keys($data)))) {
+    if (!$preserveNumericKeys && null === array_find_key($data, static fn ($v, $k) => is_string($k))) {
```

Indeed, `array_find_key` on an empty array correctly finds no string keys and returns null, making the condition true. The previous version using `ctype_digit` was false.

Commits
-------

be51e2c [Serializer] xml empty array encoding
This was referenced Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants