Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Modified http://stackoverflow.com/a/5965940/1113356, Answer
- */
- header('Content-Type: text/xml');
- $student_info[] = array(
- 'id' => '001',
- 'name' => 'Mifas',
- 'subjects' => array('English','Maths','IT')
- );
- $student_info[] = 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>");
- // function call to convert array to xml
- array_to_xml($student_info,$xml_student_info);
- //saving generated xml file
- print $xml_student_info->asXML();
- // function defination to convert array to xml
- function array_to_xml($student_info, &$xml_student_info) {
- foreach($student_info as $key => $value) {
- if(is_array($value)) {
- $key = is_numeric($key) ? "item$key" : $key;
- $subnode = $xml_student_info->addChild("$key");
- array_to_xml($value, $subnode);
- }
- else {
- $key = is_numeric($key) ? "item$key" : $key;
- $xml_student_info->addChild("$key","$value");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement