Php often encounters this situation when working as a backend server. It needs to parse xml files from the foreground and return the data in xml format. In this case, the conversion of arrays associated with xml and php is very frequent. For example, this method is often used for interaction between flex and other client programs and servers. The following are two methods I have summarized, which greatly simplifies the workload of conversion between xml and arrays.
Copy codeThe Code is as follows :/**
*
* Convert a simple array to a simple xml
* @ Param string $ array of data to be converted
* @ Param string $ tag the tag to use
* @ Example
* $ Arr = array (
'Rtxaccount' => 'Aaron ', 'ipadd' => '2017. 168.0.12 ',
'Conferencelist' => array ('conference '=>
Array (
Array ('ferenceid' => 1212, 'ferencetitle' => 'quanshi 444 ', 'smeaccount' => 'HTTP: // www.jb51.net '),
Array ('ferenceid' => 454, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'HTTP: // www.jb51.net '),
Array ('ferenceid' => 6767, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'HTTP: // www.jb51.net '),
Array ('ferenceid' => 232323, 'ferencetitle' => 'quanshi uuu ', 'smeaccount' => 'HTTP: // www.jb51.net '),
Array ('ferenceid' => 8989, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'HTTP: // www.jb51.net '),
Array ('ferenceid' => 1234343212, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'HTTP: // www.jb51.net ')
)
)
);
Convert:
<RtxAccount> aaron </rtxAccount>
<IpAddr> 192.168.0.12 </ipAddr>
<ConferenceList>
<Conference>
<ConferenceId> 1212 </conferenceId>
<ConferenceTitle> quanshi 444 </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
<Conference>
<ConferenceId> 454 </conferenceId>
<ConferenceTitle> quanshi meetting </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
<Conference>
<ConferenceId> 6767 </conferenceId>
<ConferenceTitle> quanshi meetting </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
<Conference>
<ConferenceId> 232323 </conferenceId>
<ConferenceTitle> quanshi uuu </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
<Conference>
<ConferenceId> 8989 </conferenceId>
<ConferenceTitle> quanshi meetting </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
<Conference>
<ConferenceId> 1234343212 </conferenceId>
<ConferenceTitle> quanshi meetting </conferenceTitle>
<SmeAccount> http://www.jb51.net </smeAccount>
</Conference>
</ConferenceList>
*/
Function array2xml ($ data, $ tag = '')
{
$ Xml = '';
Foreach ($ data as $ key => $ value)
{
If (is_numeric ($ key ))
{
If (is_array ($ value ))
{
$ Xml. = "<$ tag> ";
$ Xml. = array2xml ($ value );
$ Xml. = "</$ tag> ";
}
Else
{
$ Xml. = "<$ tag> $ value </$ tag> ";
}
}
Else
{
If (is_array ($ value ))
{
$ Keys = array_keys ($ value );
If (is_numeric ($ keys [0])
{
$ Xml. = array2xml ($ value, $ key );
}
Else
{
$ Xml. = "<$ key> ";
$ Xml. = array2xml ($ value );
$ Xml. = "</$ key> ";
}
}
Else
{
$ Xml. = "<$ key> $ value </$ key> ";
}
}
}
Return $ xml;
}
}
Xml2arrayCopy codeThe Code is as follows :/**
*
* Convert simple xml into an associated array
* @ Param string $ xmlString xml string
* @ Example
* <? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<RTXConferenceReqDTO>
<ConferenceTitle> IT exchange meeting </conferenceTitle>
<StartTime> 12:00:00 </startTime>
<RtxAccount> andy1111111 </rtxAccount>
<IpAddr> 192.168.1.56 </ipAddr>
<Duration> 120 </duration>
<ConferenceType> 1 </conferenceType>
<Invitees>
<Invitee>
<RtxAccount> RTX account of invitee 1 </rtxAccount>
<Tel> Number of the invitee 1 </tel>
</Invitee>
<Invitee>
<RtxAccount> RTX account of invitee 2 </rtxAccount>
<Tel> invitee 2 phone number </tel>
</Invitee>
</Invitees>
</RTXConferenceReqDTO>
Converted joined array:
Array
(
[ConferenceTitle] => IT Exchange Conference
[StartTime] => 12:00:00
[RtxAccount] => andy1111111
[IpAddr] => 192.168.1.56
[Duration] = & gt; 120
[ConferenceType] => 1
[Invitees] => Array
(
[Invitee] => Array
(
[0] => Array
(
[RtxAccount] => RTX account of invitee 1
[Tel] => Number of invitee 1
)
[1] => Array
(
[RtxAccount] => RTX account of invitee 2
[Tel] => invitee 2 phone number
)
)
)
)
*/
Function xml2array ($ xmlString = '')
{
$ TargetArray = array ();
$ XmlObject = simplexml_load_string ($ xmlString );
$ MixArray = (array) $ xmlObject;
Foreach ($ mixArray as $ key => $ value)
{
If (is_string ($ value ))
{
$ TargetArray [$ key] = $ value;
}
If (is_object ($ value ))
{
$ TargetArray [$ key] = xml2array ($ value-> asXML ());
}
If (is_array ($ value ))
{
Foreach ($ value as $ zkey => $ zvalue)
{
If (is_numeric ($ zkey ))
{
$ TargetArray [$ key] [] = xml2array ($ zvalue-> asXML ());
}
If (is_string ($ zkey ))
{
$ TargetArray [$ key] [$ zkey] = xml2array ($ zvalue-> asXML ());
}
}
}
}
Return $ targetArray;
}