Given a multi-dimensional array and the task is to convert this array into an XML file. To converting the multi-dimensional array into an xml file, create an XML file and use appendChild() and createElement() function to add array element into XML file.
Example:
- First, create a PHP Multidimensional Array for converting that array into the XML file format.
$array
=
array
(
   Â
'company'
=>
'Gfg'
,
   Â
'employe'
=>
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 user-defined function generatXML().
function
generateXML(
$data
) {
    Â
ÂÂ Â Â Â
$title
=
$data
[
'company'
];
   Â
$rowCount
=
count
(
$data
[
'employe'
]);
 Â
ÂÂ Â Â Â
// Create the xml document
   Â
$xmlDoc
=
new
DOMDocument();
 Â
ÂÂ Â Â Â
$root
=
$xmlDoc
-> appendChild(
$xmlDoc
->
                           Â
createElement(
"neveropen"
));
    Â
ÂÂ Â Â Â
$root
-> appendChild(
$xmlDoc
->Â
                       Â
createElement(
"title"
,
$title
));
   Â
$root
-> appendChild(
$xmlDoc
->Â
                 Â
createElement(
"totalRows"
,
$rowCount
));
    Â
ÂÂ Â Â Â
$tabUsers
=
$root
-> appendChild(
$xmlDoc
->
                           Â
createElement(
'rows'
));
 Â
ÂÂ Â Â Â
foreach
(
$data
[
'employe'
]
as
$user
) {
       Â
if
(!
empty
(
$user
)) {
           Â
$tabUser
=
$tabUsers
-> appendChild(
$xmlDoc
->Â
                                  Â
createElement(
'employe'
));
           Â
foreach
(
$user
as
$key
=>
$val
) {
               Â
$tabUser
-> appendChild(
$xmlDoc
->
                                 Â
createElement(
$key
,
$val
));
           Â
}
       Â
}
   Â
}
 Â
ÂÂ Â Â Â
header(
"Content-Type: text/plain"
);
 Â
ÂÂ Â Â Â
// Make the output
   Â
$xmlDoc
-> formatOutput = true;
 Â
ÂÂ Â Â Â
// Save xml file
   Â
$file_name
=
str_replace
(
' '
,
'_'
,
$title
) .
'.xml'
;
    Â
ÂÂ Â Â Â
$xmlDoc
-> save(
$file_name
);
 Â
ÂÂ Â Â Â
// Return xml file name
   Â
return
$file_name
;
}
- Then use generateXML() function and pass array data in it to convert the array to XML in PHP.
generateXML(
$array
);
- Output:
<
neveropen
>
   Â
<
title
>Gfg</
title
>
   Â
<
totalRows
>4</
totalRows
>
   Â
<
rows
>
       Â
<
employe
>
           Â
<
name
>Jatin Das</
name
>
           Â
<
age
>34</
age
>
       Â
</
employe
>
       Â
<
employe
>
           Â
<
name
>Mohit Mal</
name
>
           Â
<
age
>30</
age
>
       Â
</
employe
>
       Â
<
employe
>
           Â
<
name
>Shubham Jha</
name
>
           Â
<
age
>24</
age
>
       Â
</
employe
>
       Â
<
employe
>
           Â
<
name
>Harsha Bhosle</
name
>
           Â
<
age
>29</
age
>
       Â
</
employe
>
   Â
</
rows
>
</
neveropen
>
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!