Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Modified v2 http://stackoverflow.com/a/5965940/1113356, Answer
- */
- header('Content-Type: text/xml');
- $student_info = array(
- 0 => array(
- 'id' => '001',
- 'name' => 'Mifas',
- 'subjects' => array('English','Maths','IT')
- ),
- 1 => array(
- 'id' => '002',
- 'name' => 'Ijas',
- 'subjects' => array('Science','History','Social')
- )
- );
- // creating object of SimpleXMLElement
- $xml_student_info = new SimpleXMLElement("<?xml version=\"1.0\"?><student_info></student_info>");
- array_to_xml($student_info,$xml_student_info); // function call to convert array to xml
- print $xml_student_info->asXML(); //saving generated xml file
- // function defination to convert array to xml
- function array_to_xml($student_info, &$xml_student_info) {
- foreach($student_info as $key => $value) {
- $key = is_numeric($key) ? "item$key" : $key;
- (is_array($value))
- ? array_to_xml($value, $xml_student_info->addChild("$key"))
- : $xml_student_info->addChild("$key","$value");
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement