/ Published in: PHP

This snippet is a lambda function (anonymous function) to create an xml from an array. It uses recursiveness.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<?php $toXml = function ( &$xml, $data ) use ( &$toXml ) { foreach ( $data as $key => $value ) { $xml->addChild ( $key ); $toXml ( $xml->$key, $value ); } else { $xml->$key = $value; } } }; "key1" => "value2", "subkey1" => "subvalue1" ) ); $xml = new SimpleXMLElement ( "<root />" ); $toXml ( $xml, $data ); echo $xml->asXML();
Comments
