Array2xml and xml2array, the mutual transfer plan between xml and array. 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, this is often the case when php uses an array associated with xml and php as the backend server. it needs to parse xml files from the front end 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.
[Php]
/**
*
* 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' => 'bingxu. dong@quanshi.com '),
Array ('ferenceid' => 454, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'bingxu. dong@quanshi.com '),
Array ('ferenceid' => 6767, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'bingxu. dong@quanshi.com '),
Array ('ferenceid' => 232323, 'ferencetitle' => 'quanshi uuu ', 'smeaccount' => 'bingxu. dong@quanshi.com '),
Array ('ferenceid' => 8989, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'bingxu. dong@quanshi.com '),
Array ('ferenceid' => 1234343212, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'bingxu. dong@quanshi.com ')
)
)
);
Convert:
Aaron
192.168.0.12
1212
Quanshi 444
Bingxu.dong@quanshi.com
454
Quanshi meetting
Bingxu.dong@quanshi.com
6767
Quanshi meetting
Bingxu.dong@quanshi.com
232323
Quanshi uuu
Bingxu.dong@quanshi.com
8989
Quanshi meetting
Bingxu.dong@quanshi.com
1234343212
Quanshi meetting
Bingxu.dong@quanshi.com
*/
Function array2xml ($ data, $ tag = '')
{
$ Xml = '';
Foreach ($ data as $ key => $ value)
{
If (is_numeric ($ key ))
{
If (is_array ($ value ))
{
$ Xml. = "<$ tag> ";
$ Xml. = array2xml ($ value );
$ Xml. =" ";
}
Else
{
$ Xml. = "<$ tag> $ value ";
}
}
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. =" ";
}
}
Else
{
$ Xml. = "<$ key> $ value ";
}
}
}
Return $ xml;
}
}
Xml2array
[Php]
/**
*
* Convert simple xml into an associated array
* @ Param string $ xmlString xml string
* @ Example
*
IT exchange conference
2011-12-19 12:00:00
Andy1111111
192.168.1.56
120
1
RTX account of invitee 1
Phone Number of invitee 1
RTX account of invitee 2
Number of the invitee 2
Converted joined array:
Array
(
[ConferenceTitle] => IT Exchange Conference www.2cto.com
[StartTime] => 2011-12-19 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;
}
From the column andy1219111
...