@@ -211,6 +211,8 @@ These are the options available on the :ref:`serializer context <serializer-cont
211211 require it. Example: ``'/(firstname|lastname)/' ``
212212``ignore_empty_attributes `` (default: ``false ``)
213213 If set to true, ignores all attributes with empty values in the generated XML
214+ ``preserve_numeric_keys `` (default: ``false ``)
215+ If set to true, it keeps numeric array indexes instead of collapsing them into ``<item> `` nodes.
214216
215217.. versionadded :: 7.1
216218
@@ -224,6 +226,8 @@ These are the options available on the :ref:`serializer context <serializer-cont
224226
225227 The ``cdata_wrapping_name_pattern `` option was introduced in Symfony 7.4.
226228
229+ The ``preserve_numeric_keys `` option was introduced in Symfony 7.4.
230+
227231Example with a custom ``context ``::
228232
229233 use Symfony\Component\Serializer\Encoder\XmlEncoder;
@@ -254,6 +258,44 @@ Example with a custom ``context``::
254258 // <date>2019-10-24</date>
255259 // </track>
256260
261+ Example with ``preserve_numeric_keys ``::
262+
263+ use Symfony\Component\Serializer\Encoder\XmlEncoder;
264+
265+ $data = [
266+ 'person' => [
267+ ['firstname' => 'Benjamin', 'lastname' => 'Alexandre'],
268+ ['firstname' => 'Damien', 'lastname' => 'Clay'],
269+ ],
270+ ];
271+
272+ $xmlEncoder->encode($data, 'xml', ['preserve_numeric_keys' => false]);
273+ // outputs:
274+ //<response>
275+ // <person>
276+ // <firstname>Benjamin</firstname>
277+ // <lastname>Alexandre</lastname>
278+ // </person>
279+ // <person>
280+ // <firstname>Damien</firstname>
281+ // <lastname>Clay</lastname>
282+ // </person>
283+ //</response>
284+ $xmlEncoder->encode($data, 'xml', ['preserve_numeric_keys' => true]);
285+ // outputs:
286+ //<response>
287+ // <person>
288+ // <item key="0">
289+ // <firstname>Benjamin</firstname>
290+ // <lastname>Alexandre</lastname>
291+ // </item>
292+ // <item key="1">
293+ // <firstname>Damien</firstname>
294+ // <lastname>Clay</lastname>
295+ // </item>
296+ // </person>
297+ //</response>
298+
257299The ``YamlEncoder ``
258300-------------------
259301
0 commit comments