0

I have an array in PHP which is currently populated by manually setting the array line by line in a loop. I increment each 'item' with a simple counter currently, which is what makes each item appear grouped together under that counter. For example:

$item[items][$x][color] = $getColor;
$item[items][$x][size] = $getSize;

Inside of a loop. This results in XML that looks like the following:

<items>
  <1>
   <color>Blue</color>
   <size>XL</size>
  </1>
  <2>
   <color>Red</color>
   <size>M</size>
  </2>
</items>

This is completely fine, except in order for the data to be read, those numbers need to be something like '' every time, with multiple occurrences. Obviously if I replaced the $x with "shirt" it would be overwritten each time [would work the first time, but all subsequent runs would

I know I'm missing something simple here, and this is existing code so the preference would be to change the initial array somehow vs. simpleXML operations if possible.

Ideal output would look like this:

<iems>
  <shirt>
   <color>Blue</color>
   <size>XL</size>
  </shirt>
  <shirt>
   <color>Red</color>
   <size>M</size>
  </shirt>
</items>
BSDG
  • 39
  • 8
  • PD https://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml – splash58 Jul 30 '19 at 11:10
  • For posterity, none of the solutions in there worked for the above specific issue. However, buried in the notes was a single revision that did. That one is here: https://stackoverflow.com/revisions/5965940/2 – BSDG Jul 30 '19 at 14:30
  • Possible duplicate of [How to convert array to SimpleXML](https://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml) – miken32 Aug 14 '19 at 19:23

0 Answers0