@@ -211,6 +211,8 @@ These are the options available on the :ref:`serializer context <serializer-cont
211
211
require it. Example: ``'/(firstname|lastname)/' ``
212
212
``ignore_empty_attributes `` (default: ``false ``)
213
213
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.
214
216
215
217
.. versionadded :: 7.1
216
218
@@ -224,6 +226,8 @@ These are the options available on the :ref:`serializer context <serializer-cont
224
226
225
227
The ``cdata_wrapping_name_pattern `` option was introduced in Symfony 7.4.
226
228
229
+ The ``preserve_numeric_keys `` option was introduced in Symfony 7.4.
230
+
227
231
Example with a custom ``context ``::
228
232
229
233
use Symfony\Component\Serializer\Encoder\XmlEncoder;
@@ -254,6 +258,44 @@ Example with a custom ``context``::
254
258
// <date>2019-10-24</date>
255
259
// </track>
256
260
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
+
257
299
The ``YamlEncoder ``
258
300
-------------------
259
301
0 commit comments