I have an array which I need to convert to XML with SimpleXML. The method below is almost doing the work but there's one trouble with it. It can't generate structure like this:
$xmlFields = array(
'rootElt' => array(
'field1' => '',
'field2' => '',
'field3' => array(
'field4' => array(
'income' => array(
'owner' => '',
'description' => '',
),
'income' => array(
'owner' => '',
'description' => '',
),
),
)
)
);
It writes only the last 'income' of section 'field4' but I need the output like:
<field4>
<income>
<owner>....</owner>
<description>....</description>
</income>
<income>
<owner>....</owner>
<description>....</description>
</income>
</field4>
Could someone help me fix this function:
/**
* @param array $dataArr
* @param SimpleXMLElement $xmlObj
*/
private function array2xml( $dataArr, $xmlObj ) {
foreach ( $dataArr as $key => $value ) {
if ( is_array($value) ) {
if ( !is_numeric($key) ) {
$subnode = $xmlObj->addChild( $key );
self::array2xml( $value, $subnode );
} else {
self::array2xml( $value, $xmlObj );
}
} else {
$xmlObj->addChild( $key, $value );
}
}
}