Change language

Convert multidimensional array to XML file in PHP

Example :
  • First create a PHP multidimensional array to convert this array to XML file format. $array = array ( ’company’ = > ’ Gfg’ , ’employee’ = > array ( ’0’ = > array ( ’ name’ = > ’Jatin Das’ , ’age’ = > ’34’ ), ’1’ = > array ( ’name’ = > ’ Mohit Mal’ , ’age’ = > ’ 30’ ), ’ 2’ = > array ( ’name’ = > ’ Shubham Jha’ , ’age’ = > ’ 24’ ), ’ 3’ = > array ( ’name’ = > ’ Harsha Bhosle’ , ’age’ = > ’ 29’ ) ) );
  • Now you need to create a custom generatXML() function. function generateXML ( $data ) { $title = $data [ ’company’ ]; $rowCount = count ( $data [ ’employees’ ]); // Create an xml document $xmlDoc = new DOMDocument(); $root = $xmlDoc -> appendChild ( $xmlDoc -> createElement ( "engineer" )); $root -> appendChild ( $xmlDoc -> createElement ( "title" , $title )); $root -> appendChild ( $xmlDoc -> createElement ( "totalRows" , $rowCount )); $tabUsers = $root -> appendChild ( $xmlDoc -> createElement ( ’rows’ )); foreach ( $data [ ’employees’ ] as $user ) { if (! empty ( $user )) { $tabUser = $tabUsers -> appendChild ( $xmlDoc -> createElement ( ’employee’ )); foreach ( $user as $key = > $val ) { $tabUser -> appendChild ( $xmlDoc -> createElement ( $key , $val )); } } } header ( "Content-Type: text / plain" ); // Make output $xmlDoc -> formatOutput = true; // Save XML file $file_name = str_replace ( ’’ , ’_’ , $title ). ’.xml’ ; $xmlDoc -> save ( $file_name ); // Return the xml file name return $file_name ; }
  • Then use the generateXML() function and pass the array data into it to convert the array to XML in PHP. generateXML ( $array );
  • Exit: < engineer > < title > Gfg < / title > < totalRows > 4 < / totalRows > < rows > < employees > < name > Jatin Das < / name > < age > 34 < / age > < / employees > < employees > < name > Mohit Mal < / name > < age > 30 < / age > < / employees > < employees > < name > Shubham Jha < / name > < age > 24 < / age > < / employees > < employees > < name > Harsha Bhosle < / name > < age > 29 < / age > < / employees > < / rows > < / engineer >

Shop

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Best laptop for engineering student

$

Best laptop for development

$

Best laptop for Cricut Maker

$

Best laptop for hacking

$890

Best laptop for Machine Learning

$950

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method