This article introduces two simple examples: Converting php arrays to xml and xml arrays. I hope this article will help you.
Php array to xml
| The Code is as follows: |
Copy code |
|
Function array2xml ($ array, $ xml = false ){
If ($ xml = false ){
$ Xml = new SimpleXMLElement ('<root/> ');
}
Foreach ($ array as $ key => $ value ){
If (is_array ($ value )){
Array2xml ($ value, $ xml-> addChild ($ key ));
} Else {
$ Xml-> addChild ($ key, $ value );
}
}
Return $ xml-> asXML ();
}
Header ('content-type: text/xml ');
Print array2xml ($ array ); |
Php xml to array
| The Code is as follows: |
Copy code |
|
$ S = <EOS
<Root>
<Formula>
<Formulaname> Basic </formulaname>
<Movespeed> 1 </movespeed>
<Box> 4 </box>
<Chicken> 3 </chicken>
<Ducks> 1 </ducks>
<Cereal> 2 </cereal>
</Formula>
</Root>
EOS;
$ A = json_decode (json_encode (array) simplexml_load_string ($ s), 1 );
Print_r ($ ); |