I need to convert an XML file to an array using some really simple and quick solution. This one was looking good to me:
json_decode(json_encode(simplexml_load_string($xmlContent, "SimpleXMLElement", LIBXML_NOCDATA)), true);
The problem with this is that it treats multiple children as an indexed array consisting of an associative array per child, but if it's only one child in a parent tag, it doesn't create an indexed array with one element and put an assoc array directly to the parent tag element. I need the output of these scenarios to be consistent. I.e. an indexed array first not depending on the quantity of the children. My aim is a consistent schema after parsing.
An example of multiple children:
Input:
<PICK_NOTE_LINES>
<LINE>
<PICK_LINE_NUM>1</PICK_LINE_NUM>
<PRODUCT_CODE>3342</PRODUCT_CODE>
<BATCH_CODE></BATCH_CODE>
<QUANTITY>1</QUANTITY>
</LINE>
<LINE>
<PICK_LINE_NUM>2</PICK_LINE_NUM>
<PRODUCT_CODE>5285</PRODUCT_CODE>
<BATCH_CODE></BATCH_CODE>
<QUANTITY>1</QUANTITY>
</LINE>
</PICK_NOTE_LINES>
Output (am indexed array in the LINE parent element):
['PICK_NOTE_LINES'=>['LINE'=>[['PICK_LINE_NUM'=>1, ...],['PICK_LINE_NUM'=>2, ...]]]
An example of one child:
<PICK_NOTE_LINES>
<LINE>
<PICK_LINE_NUM>1</PICK_LINE_NUM>
<PRODUCT_CODE>3342</PRODUCT_CODE>
<BATCH_CODE></BATCH_CODE>
<QUANTITY>1</QUANTITY>
</LINE>
</PICK_NOTE_LINES>
output (an associative array in the line parent element):
['PICK_NOTE_LINES'=> ['LINE'=>['PICK_LINE_NUM'=>1, ...]]]
Is it possible to get it always putting children into an indexed array even if it's only one child? If it's not possible, what can I use to get a consistent output?