Thanks to visit codestin.com
Credit goes to github.com

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.

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