Im having some trouble with creating an Multidimensional SimpleXMLElement, it works well with simple Array's but when there are multidimensional Array the SimpleXMLElement wont write in multid SimpleXMLEelement. Like this:
createXML function:
public function createXML($data, $root = null) {
$xml = new \SimpleXMLElement($root ? '<' . $root . '/>' : '<marathon/>');
array_walk_recursive($data, function($value, $key)use($xml){
$xml->addChild($key, $value);
});
$dom = dom_import_simplexml($xml)->ownerDocument;
$dom->encoding = "UTF-8";
$dom->formatOutput = true;
print $dom->saveXML();
}
This prints:
<marathon>
<media_id>FACE</media_id>
<agreement_id>****</agreement_id>
<client_reference>123456</client_reference>
<client_contact>Asim</client_contact>
<plan_number>407</plan_number>
<plan_name>TEST</plan_name>
<cuid>123456</cuid>
<status>P</status>
<colour>0</colour>
<insertion_date>2018-09-19</insertion_date>
<end_date>2018-09-20</end_date>
<client_reference>1234</client_reference>
<price_code>0</price_code>
<number_of_units>250000</number_of_units>
<gross>1000</gross>
<comment>THIS IS A TEST DO NOT FAKTURER</comment>
</marathon>
When what i want is:
<marathon>
<media_id>FACE</media_id>
<agreement_id>REDP</agreement_id>
<client_reference>123456</client_reference>
<client_contact>Asim Tariq</client_contact>
<plan_number>407</plan_number>
<plan_name>TEST</plan_name>
<cuid>123456</cuid>
<status>P</status>
<colour>0</colour>
<insertion>
<insertion_date>2018-09-19</insertion_date>
<end_date>2018-09-20</end_date>
<client_reference>1234</client_reference>
<price_row>
<price_code>0</price_code>
<number_of_units>250000</number_of_units>
<gross>1000</gross>
<comment>THIS IS A TEST DO NOT FAKTURER</comment>
</price_row>
</insertion>
</marathon>
How can i manage this with the code i have in createXML
? The XML does not get multidimensional?
This is the Array i send in as $data:
[
"media_id" => 'FACE',
"agreement_id" => 'REDP',
"client_reference" => 123456,
"client_contact" => "Asim Tariq",
"plan_number" => 407,
"plan_name" => "TEST",
"cuid" => 123456,
"status" => 'P',
"colour" => 0,
"insertion" => [
"insertion_date" => '2018-09-19',
"end_date" => '2018-09-20',
"client_reference" => 1234,
"price_row" => [
"price_code" => 000,
"number_of_units" => 250000,
"gross" => 1000,
"discount" => [
],
"comment" => "THIS IS A TEST DO NOT FAKTURER",
],
],
]