Discussion: mutual conversion between array2xml, xml2array, and xml and array _ PHP

Source: Internet
Author: User
This article provides a detailed analysis of the mutual conversion between array2xml, xml2array, and xml and array. if you want to use php as a backend server, you will often encounter this situation, the xml file from the front-end needs to be parsed and the data is returned 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.
The 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.bitsCN.com '),
Array ('ferenceid' => 454, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'http: // www.bitsCN.com '),
Array ('ferenceid' => 6767, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'http: // www.bitsCN.com '),
Array ('ferenceid' => 232323, 'ferencetitle' => 'quanshi uuu ', 'smeaccount' => 'http: // www.bitsCN.com '),
Array ('ferenceid' => 8989, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'http: // www.bitsCN.com '),
Array ('ferenceid' => 1234343212, 'ferencetitle' => 'quanshi meetting ', 'smeaccount' => 'http: // www.bitsCN.com ')
)
)
);
Convert:
Aaron
192.168.0.12


1212
Quanshi 444
Http://www.bitsCN.com </smeAccount>


454
Quanshi meetting
Http://www.bitsCN.com </smeAccount>


6767
Quanshi meetting
Http://www.bitsCN.com </smeAccount>


232323
Quanshi uuu
Http://www.bitsCN.com </smeAccount>


8989
Quanshi meetting
Http://www.bitsCN.com </smeAccount>


1234343212
Quanshi meetting
Http://www.bitsCN.com </smeAccount>


*/
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
The code is as follows:
/**
*
* 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
[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;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.