Convert associative arrays into XML in PHP
In this article, we will explain to you how to convert associative arrays into XML in PHP. sometimes we need to convert associative arrays into XML file in PHP. so here we use the XMLWriter class to write an XML file into PHP.
If you want to store dynamic data into an XML file then we can easily store data in an XML file using PHP. so you can see our following example.
Create Associative Array
In this step, We need to associative the array for convert the array to XML. so here first we will create an associative array. for that, you can following example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $userData = array( 'title' => 'User Details', 'totalUser' => 4, 'user_records' => array( 'user' => array( array('firstname' => 'john', 'lastname' => 'doe', 'username' => 'jd123'), array('firstname' => 'ajay', 'lastname' => 'devgan', 'username' => 'ad123'), array('firstname' => 'rohit', 'lastname' => 'sharma', 'username' => 'rs123'), array('firstname' => 'kapil', 'lastname' => 'sharma', 'username' => 'ks123') ) ) ); ?> |
Convert associative array to XML
In this step, We will convert the associative array to XML using the createXMLFile function.
1 2 3 4 5 6 7 8 9 10 11 | function createXMLFile( XMLWriter $xml, $data ) { foreach( $data as $key => $value ) { if( is_array( $value )) { $xml->startElement( $key ); createXMLFile( $xml, $value ); $xml->endElement(); continue; } $xml->writeElement( $key, $value ); } } |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <?php $userData = array( 'title' => 'User Details', 'totalUser' => 4, 'user_records' => array( 'user' => array( array('firstname' => 'john', 'lastname' => 'doe', 'username' => 'jd123'), array('firstname' => 'ajay', 'lastname' => 'devgan', 'username' => 'ad123'), array('firstname' => 'rohit', 'lastname' => 'sharma', 'username' => 'rs123'), array('firstname' => 'kapil', 'lastname' => 'sharma', 'username' => 'ks123') ) ) ); $xml = new XmlWriter(); $xml->openURI('user.xml'); $xml->openMemory(); $xml->startDocument('1.0', 'utf-8'); $xml->startElement('user_details') ; createXMLFile($xml,$userData); $xml->endElement(); function createXMLFile(XMLWriter $xml,$data) { foreach($data as $key => $value) { if(is_array($value)) { $xml->startElement($key); createXMLFile($xml,$value); $xml->endElement(); continue; } $xml->writeElement($key,$value); } } ?> |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <?xml version="1.0"?> <user_details> <title>User Details</title> <totalUser>4</totalUser> <user_records> <user> <firstname>john</firstname> <lastname>doe</lastname> <username>john123</username> </user> <user> <firstname>ajay</firstname> <lastname>devgan</lastname> <username>ajdev</username> </user> <user> <firstname>rohit</firstname> <lastname>sharma</lastname> <username>rs123</username> </user> <user> <firstname>kapil</firstname> <lastname>sharma</lastname> <username>ks123</username> </user> </user_records> </user_details> |
Read Also
Convert XML into Associative Array in PHP
How to Remove file Extension from Filename in PHP
How to remove duplicate values from an array in PHP